Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

[WPF] Control Not Updating with Bound Value Change

$
0
0

I've been developing in Silverlight for years, but I've had to switch to WPF for a simple sample app that connects to our WCF services. I've coded it up just like I would in Silverlight, but for some reason, the changes that I'm making to the underlying DataContext are not being reflected in the controls. The binding is TwoWay, so what am I doing wrong?

Firstly, this is my DataContext:

        private TaskInfoCustom TaskInfo
        {
            get
            {
                return (TaskInfoCustom)DataContext;
            }
            set
            {
                DataContext = value;
                value.PropertyChanged += Value_PropertyChanged;
                SaveButton.IsEnabled = value != null;
            }
        }

        private void Value_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.PropertyName);
        }


Here is a control that is bound to the ProbCode property:

<ComboBox  Grid.Row="2" Grid.Column="1"   HorizontalAlignment="Left" Width="200"  IsReadOnly="True" Margin="4"  VerticalAlignment="Stretch" DisplayMemberPath="Description" Name="ProblemBox" SelectedItem="{Binding ProbCode, Mode=TwoWay}" />


This code works as expected. If I change a value in the ComboBox, I can see in the Output window that the changed property is "ProbCode". However, when I call this code, the changed value is not reflected by the ComboBox:

        private void ClearProblem_Click(object sender, RoutedEventArgs e)
        {
            TaskInfo.ProbCode = null;
            if (ProblemBox.SelectedItem == null)
            {
                System.Diagnostics.Debug.WriteLine("It's null!");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(ProblemBox.SelectedItem.ToString());
            }
        }

From working with Silverlight, I would expect the ComboBox's SelectedItem property to become null, and for the display to go empty. Furthermore, when I click the clear button for the event handler above, I see that the Output window outputs "ProbCode". The SelectedItem remains not null. Why? I can't see any reason why the ComboBox wouldn't be responding to the PropertyChanged event...

I should also note that if I reload the record and set the DataContext, then the ComboBox is reset the first time. It just doesn't work the second time.

What am I doing wrong?





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>