Matthew Kruskamp
Matthew Kruskamp

Reputation: 1887

WPF Inner Property Binding not updating

I have an INotifyProperty Screen item that I have bound to a wpf control.

Ok... I Simplified everything and am posting more code. I have a MainViewModel with the selected screen property.

public Screen SelectedScreen
    {
        get { return this.selectedScreen; }
        set
        {
            this.selectedScreen = value;
            this.OnPropertyChanged("SelectedScreen");
        }
    }

I have a textbox that is bound to this property:

<TextBlock Text="{Binding Path=SelectedScreen.ScreenNumber}" />

This all works initially. I have created another control that is changing the selected screen with the following code.

public Screen SelectedScreen
    {
        get { return (Screen)GetValue(SelectedScreenProperty); }
        set 
        { 
            this.SetValue(SelectedScreenProperty, value);

            for (int x = 0; x < this.Screens.Count; ++x)
                this.Screens[x].IsSelected = false;
            value.IsSelected = true;
        }
    }

    public ObservableCollection<Screen> Screens
    {
        get { return (ObservableCollection<Screen>)GetValue(ScreensProperty); }
        set { this.SetValue(ScreensProperty, value); }
    }

    public static readonly DependencyProperty SelectedScreenProperty =
        DependencyProperty.Register("SelectedScreen", 
            typeof(Screen), 
            typeof(ScreenSelection));

    public static readonly DependencyProperty ScreensProperty =
        DependencyProperty.Register("Screens",
            typeof(ObservableCollection<Screen>), 
            typeof(ScreenSelection),
            new UIPropertyMetadata(new ObservableCollection<Screen>()));

This screen selection control is working. When I change screens and put a breakpoint on the set property of SelectedScreen it is called which then calls the SelectedScreen property of the MainViewModel. So the event is firing, but the textbox isn't updated even though it binds correctly the first time.

Upvotes: 1

Views: 775

Answers (2)

Matthew Kruskamp
Matthew Kruskamp

Reputation: 1887

Thank you gehho for looking at this. I figured it out and there is no way you had enough information to be able too. I was inheriting from ViewModelBase in the MainViewModel that was inheriting from ObservableObject where I implemented INotifyPropertyChanged. The problem is that I implemented the methods for INotifyPropertyChanged in both classes and WPF was listening to the wrong one. Very obscure. Very annoying. Very lasjkdf;ashdoh

Upvotes: 0

gehho
gehho

Reputation: 9238

Does the class which contains the SelectedScreen property implement INotifyPropertyChanged? When the SelectedScreen property changes, the containing class should raise the PropertyChanged event, and typically, WPF should update the Binding.

Upvotes: 1

Related Questions