[英]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.
我没有使用WPF的丰富经验,团队中的其他人也没有,所以我确定自己犯了一个愚蠢的错误,并且看不到它。 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.
我一直在为这个项目添加新功能,并且已经将意大利面条代码转换为MVVM解决方案的开始。 I have a model, which derives from the DeSerialization of an XML doc, and the ViewModel.
我有一个模型,该模型派生自XML文档的反序列化和ViewModel。 They both currently have data I need for a UserControl.
它们当前都具有我需要UserControl的数据。 So I have the DataContext set to the Code Behind.
因此,我将DataContext设置为“隐藏代码”。 In this MsClassification is initially set to "UN".
在此,MsClassification最初设置为“ 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.
我将组合框更改为其他内容,然后使用按钮重新读取XML,从而将MsClassification设置回“ UN”,但是,视图不会更新。 Interestingly enough, if I set to MsLoggingLevel to something else, and re-read the XML, it will update, both values.
有趣的是,如果我将MsLoggingLevel设置为其他值,然后重新读取XML,它将更新两个值。
XAML: 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 确保在ViewModel中实现INotifyPropertyChanged,并将绑定设置为TwoWay
{Binding Path=ClassificationLevels, Mode=TwoWay}
Also one other thing, ... Try using Fody . 还有另一件事,...尝试使用Fody 。 It will save you a ton of time Try using WPF Inspector .
这将为您节省大量时间。尝试使用WPF Inspector 。 It gives you much more options to debug bindings and update.
它为您提供了更多调试绑定和更新的选项。 You can inspect the Application more in detail.
您可以更详细地检查应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.