Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having trouble understanding why a databinding in a usercontrol isn't updating. Since it's easier to show than explain, here's some sample code I wrote to demonstrate.

MyClass will be my model with a single property, MyInt (I know, not very original). Pretty basic.
C#
public class MyClass
{
    private int _myInt;

    public int MyInt
    {
        get { return _myInt; }
        set { _myInt = value; }
    }
}


A UserControl with a single textblock whose text is bound to the MyInt property of a custom dependency property of type MyClass

XAML
XML
<Grid x:Name="root">
    <TextBlock Text="{Binding UserControlMyClass.MyInt}" />
</Grid>


Code-behind
C#
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        root.DataContext = this;
    }

    public MyClass UserControlMyClass
    {
        get { return (MyClass)GetValue(UserControlMyClassProperty); }
        set { SetValue(UserControlMyClassProperty, value); }
    }

    // Using a DependencyProperty as the backing store for UserControlMyClass.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UserControlMyClassProperty =
        DependencyProperty.Register("UserControlMyClass", typeof(MyClass), typeof(UserControl1), new PropertyMetadata(null));
}


The Main Window with a textblock to show that it does update and the user control which doesn't update.
XML
<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<Grid>
    <TextBlock Text="{Binding ViewModelMyClass.MyInt}" />
    <local:UserControl1 UserControlMyClass="{Binding ViewModelMyClass}" HorizontalAlignment="Center" />
</Grid>


The view model backing the window. It contains a property of type MyClass that will be displayed in the textblock and user control. There is a timer that just increments the MyInt property and calls property changed for the view model property.
C#
public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        _timer.Elapsed += _timer_Elapsed;
        _timer.Start();
    }

    private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        this.ViewModelMyClass.MyInt++;
        OnPropertyChanged("ViewModelMyClass");
    }

    private System.Timers.Timer _timer = new System.Timers.Timer(1000);

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    private MyClass _viewModelMyClass = new MyClass();
    public MyClass ViewModelMyClass
    {
        get { return _viewModelMyClass; }
    }
}


When I run this, the textblock on the window updates every second as expected. The user control shows the initial value for MyInt but will not update.

If the dependency property in the user control was an int and I bound that to ViewModelMyClass.MyInt, then it will update, just like the textblock.

Of course, if I add a property notification for MyInt in the MyClass model then that will work too. I just want to know if someone can explain why the textblock on the main window updates but the user control won't.

Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900