WPF - Two way binding use a user control...binding to object, not an element!

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-05-24T02:20:00Z Indexed on 2010/05/24 2:31 UTC
Read the original article Hit count: 253

Filed under:

I created an object with a simple property with a default value. I then created a user control that has a text box in it. I set the datacontext of the user control to the object.

The text box correctly shows the properties default value but I can't seem to update the property value when the user changes the text box value. I created a simple project to illustrate my code.

Thanks for the help!!

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private string _titleValue;

        public string TitleValue
        {
            get
            {
                return _titleValue;
            }
            set 
            {
                _titleValue = value;
                textBox1.Text = _titleValue;
            }
        }

        public static readonly DependencyProperty TitleValueProperty = DependencyProperty.Register(
           "TitleValue", typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata(new PropertyChangedCallback(titleUpdated))
           );

//Don't think I should need to do this!!!
        private static void titleUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((UserControl1)d).TitleValue = (string)e.NewValue; 
        }
    }

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="94,97,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"
                  Text="{Binding Path=TitleValue, Mode=TwoWay}"/>
    </Grid>
</UserControl>

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                var dummy = new DummyObject("This is my title.");
                userControl11.DataContext = dummy;

            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("The value is: " + ((DummyObject)userControl11.DataContext).Title);
            }
        }

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
        <Grid>
            <my:UserControl1 HorizontalAlignment="Left" Margin="95,44,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="191" Width="293" 
                             TitleValue="{Binding Path=Title, Mode=TwoWay}"/>
            <Button Content="Check Value" Height="23" HorizontalAlignment="Left" Margin="20,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>
    </Window>

© Stack Overflow or respective owner

Related posts about wpf