Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:32:14+00:00 2026-05-26T03:32:14+00:00

I have a Window, containing a UserControl ‘TemplateEditor’. The (shortened) TemplateEditor XAML is: <UserControl

  • 0

I have a Window, containing a UserControl ‘TemplateEditor’. The (shortened) TemplateEditor XAML is:

<UserControl x:Class="xxx.Windows.Core.Controls.TemplateEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             x:Name="userControl">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Grid.Row="1" x:Name="textBox" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" />
    </Grid>
</UserControl>

I want to be able to bind data through the TemplateEditor into the TextBox “textBox”. I’m using a DependencyProperty to mask the TextBox in the code-behind:

namespace xxx.Windows.Core.Controls
{
    public partial class TemplateEditor : UserControl 
    {
        public string Text
        {
            get 
            { 
                string s=(string)GetValue(TextProperty);
                return s;
            }
            set 
            {
                if (Text != value)
                { 
                    SetValue(TextProperty, value);
                }
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TemplateEditor),new PropertyMetadata(null,Text_PropertyChanged));

        public TemplateEditor()
        {
            InitializeComponent();
        }

        private static void Text_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ((TemplateEditor)source).textBox.Text = (string)e.NewValue;
        } 
    }
}

So from that you can see I have a DependencyProperty Text that I have a callback on to pick up changes made by to the binding (say, from the ViewModel) and apply to the TextBox value.

This works.

The problem is, I can’t seem to have the binding work in reverse, ie. to get the value back out of the Text property (and therefore the TextBox) and back in to the binding consumer (the ViewModel). I’ve debugged calling GetValue(TextProperty) and this returns the correct value so the DP dictionary is correctly updating.

Given the following Binding in the parent Window’s XAML:

<src:ApplicationWindowBase x:Class="xxx.Windows.Client.Filing"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:src="clr-namespace:xxx.Windows.Core;assembly=MIGTurbo1.Windows.Core"
        xmlns:viewmodel="clr-namespace:xxx.Windows.Client.ViewModel"
        xmlns:converters="clr-namespace:xxx.Windows.Client.Converters"
        xmlns:xxx_Windows_Core_Controls="clr-namespace:xxx.Windows.Core.Controls;assembly=xxx.Windows.Core" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        Title="File Item(s)" Height="450" Width="550" ShowInTaskbar="False">
    <src:ApplicationWindowBase.Resources>
        <viewmodel:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
        <converters:FileableItemNameStringConverter x:Key="fileableItemNameStringConverter" />
        <converters:FileableItemTypeStringConverter x:Key="fileableItemTypeStringConverter" />
        <converters:FileableItemMetaDataStringConverter x:Key="fileableItemMetaDataStringConverter" />
        <converters:FileableItemIconConverter x:Key="fileableItemIconConverter" />
    </src:ApplicationWindowBase.Resources>
    <src:ApplicationWindowBase.DataContext>
        <Binding Mode="OneWay" Path="Filing" Source="{StaticResource ViewModelLocator}"/>
    </src:ApplicationWindowBase.DataContext>
    <Grid>
       <!-- SNIP -->
    <xxx_Windows_Core_Controls:TemplateEditor Grid.Column="1" Grid.Row="1" Margin="0" Text="{Binding Comment, Mode=TwoWay}" d:LayoutOverrides="Width, Height"/>
       <!-- SNIP -->
</src:ApplicationWindowBase>

I am using MVVM Light and the ViewModel does bind correctly. There are other control/field bindings (ommitted) that work fine. THe problem is that the Text property binding on the Comment ViewModel property is not working. I can set it fine (ie. set value in ViewModel, then bind) but the user-entered value in Text never goes into the ViewModel Comments property.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:32:15+00:00Added an answer on May 26, 2026 at 3:32 am

    Ok, typical StackOverflow usage pattern adopted.

    I realised I am writing this one-way, have added a TextChanged event handler to update the dependency property.

    public partial class TemplateEditor : UserControl 
    {
    
        public string Text
        {
            get 
            { 
                string s=(string)GetValue(TextProperty);
                return s;
            }
            set 
            {
                if (Text != value)
                { 
                    SetValue(TextProperty, value);
                }
            }
        }
    
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TemplateEditor),new PropertyMetadata(null,Text_PropertyChanged));
    
        public TemplateEditor()
        {
            InitializeComponent();
    
            textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
        }
    
        private static void Text_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ((TemplateEditor)source).textBox.Text = (string)e.NewValue;
        }
    
    
        void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Text = textBox.Text;
        }
    }
    

    Thanks for your time, and apologies. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (parent) window containing JQuery tabs: <div id=tabs> <ul class=tabHeader> <li><a href=?ctrl=doSomething
I have a Windows Form window containing a FlowLayoutPanel, an OK Button, and a
I have a general question. I would like to have a window containing some
The Scenario I have a windows forms application containing a MAINFORM with a listbox
I have a Windows Forms app, that has a single ElementHost containing a WPF
In our application, we have a window containing a Grid and two buttons -
I have a simple UserControl containing Label, ComboBox and Button. In brief, it is
I have a UserControl containing TextBoxes and ComboBoxes and this UserControl is contained in
I have a window containing an iframe (same origin), so scripts from this iframe
Let's say I have a panel/window containing a search form and tab(s) containing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.