tags:

views:

49

answers:

2

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.

+1  A: 

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.

gehho
The Screen class does contain INotifyPropertyChanged. So does the containing class bound to the datacontext. When I swap out the SelectedScreen, INotifyPropertyChanged does get called.
Matthew Kruskamp
Do you have some code of your containing class? The `Screen` class is not important here, as it is the `SelectedScreen` property which is changed, not the `VisualizationTypes`, as you pointed out. So, could you post the code of the containing class, especially the `SelectedScreen` property?!
gehho
I updated the code so that you can see what it is doing.
Matthew Kruskamp
A: 

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

Matthew Kruskamp
Oh well... that's strange. But then you should have got a compiler warning, did not you? It should have said that the `PropertyChanged` event is declared twice, and that you should use the `new` keyword if this was done on purpose, right?! Or am I missing something here?
gehho