简体   繁体   中英

WPF Binding Not Updating For One Property

I know there are a lot of questions about this, I have ensured every answer has been attempted. I do not have much experience with WPF, nor does anyone else on my team, so I am sure I made a stupid error, and cannot see it. I have been adding a new feature to this project that I have taken over, and have been transforming the spaghetti code to the start of a MVVM solution. I have a model, which derives from the DeSerialization of an XML doc, and the ViewModel. They both currently have data I need for a UserControl. So I have the DataContext set to the Code Behind. In this MsClassification is initially set to "UN". I change the combobox to something else, and use a button to re-read the XML, which sets MsClassification back to "UN", however, the View does not update. Interestingly enough, if I set to MsLoggingLevel to something else, and re-read the XML, it will update, both values.

XAML:

<TabItem Header="Init" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="78" Margin="0" ClipToBounds="True">
            <Grid Background="#FFE5E5E5" ClipToBounds="True">
                <TreeView x:Name="AssessorToolWindow_InitTree" Margin="0" BorderThickness="0" ClipToBounds="True" DataContext="{Binding Path=VSParentReference.InitRules.Init}">
                    <TreeViewItem  Header="Excluded Files" ItemsSource="{Binding Path=MsExcludedFiles}">
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <TreeViewItem Header ="{Binding}"/>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                    <TreeViewItem Header="Included Files" ItemsSource="{Binding Path=MsIncludedFiles, Mode=TwoWay}">
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <TreeViewItem Header="{Binding}"/>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                    <TreeViewItem Header="Project Path">
                        <TreeViewItem Header="{Binding Path=MsRootProjectDirectory, Mode=TwoWay}"/>
                    </TreeViewItem>
                    <TreeViewItem Header="Classification">
                        <ComboBox 
                            ItemsSource="{Binding Path=ClassificationLevels}"
                            SelectedValue="{Binding Path=MsClassification}"
                            IsSynchronizedWithCurrentItem="True" />
                    </TreeViewItem>
                    <TreeViewItem Header="Log Level">
                        <ComboBox 
                            ItemsSource="{Binding Path=LoggerLevels}"
                            SelectedItem="{Binding Path=MsLogLevel}"
                            IsSynchronizedWithCurrentItem="True"
                            />
                    </TreeViewItem> 
                </TreeView>
            </Grid>
        </TabItem>

Code-behind:

        [XmlArrayItem("EXC"),
    XmlArray("ExcludedFiles")]
    public ObservableCollection<string> MsExcludedFiles
    {
        get
        {
            return _msExcludedFiles;

        }
        set
        {
            if (value != _msExcludedFiles)
            {
                _msExcludedFiles = value;
                NotifyPropertyChanged("MsExcludedFiles");
            }

        }
    }

    [XmlArrayItem("INC"),
    XmlArray("IncludedFiles")]
    public ObservableCollection<string> MsIncludedFiles
    {
        get
        {
            return _msIncludedFiles;
        }
        set
        {
            if (value != _msIncludedFiles)
            {
                _msIncludedFiles = value;
                NotifyPropertyChanged("MsIncludedFiles");
            }
        }
    }

    [XmlElement(ElementName = "ProjectPath")]
    public string MsRootProjectDirectory
    {
        get
        {
            return _msRootProjectDirectory;
        }
        set
        {
            if (value != _msRootProjectDirectory)
            {
                _msRootProjectDirectory = value;
                NotifyPropertyChanged("MsRootProjectDirectory");
            }
        }
    }

    [XmlElement(ElementName = "LogLevel")]
    public string MsLogLevel
    {
        get { return _msLogLevel; }
        set 
        {
            if (value != _msLogLevel)
            {
                _msLogLevel = value;
                NotifyPropertyChanged("MsLogLevel");
            }      
        }
    }


    [XmlElement(ElementName = "Classification")]
    public String MsClassification
    {
        get
        {
            return classification;
        }
        set
        {
            if (value != classification)
            {
                classification = value;
                NotifyPropertyChanged("MsClassification");
            }
        }
    }
    protected virtual void NotifyPropertyChanged(string p)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(p));
    }

Make sure to implement in the ViewModel INotifyPropertyChanged, and set the binding to TwoWay

{Binding Path=ClassificationLevels, Mode=TwoWay}

Also one other thing, ... Try using Fody . It will save you a ton of time Try using WPF Inspector . It gives you much more options to debug bindings and update. You can inspect the Application more in detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM