[英]WPF Binding Not Updating For One Property
我知道对此有很多问题,我已经确保尝试了所有答案。 我没有使用WPF的丰富经验,团队中的其他人也没有,所以我确定自己犯了一个愚蠢的错误,并且看不到它。 我一直在为这个项目添加新功能,并且已经将意大利面条代码转换为MVVM解决方案的开始。 我有一个模型,该模型派生自XML文档的反序列化和ViewModel。 它们当前都具有我需要UserControl的数据。 因此,我将DataContext设置为“隐藏代码”。 在此,MsClassification最初设置为“ UN”。 我将组合框更改为其他内容,然后使用按钮重新读取XML,从而将MsClassification设置回“ UN”,但是,视图不会更新。 有趣的是,如果我将MsLoggingLevel设置为其他值,然后重新读取XML,它将更新两个值。
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>
后台代码:
[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));
}
确保在ViewModel中实现INotifyPropertyChanged,并将绑定设置为TwoWay
{Binding Path=ClassificationLevels, Mode=TwoWay}
还有另一件事,...尝试使用Fody 。 这将为您节省大量时间。尝试使用WPF Inspector 。 它为您提供了更多调试绑定和更新的选项。 您可以更详细地检查应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.