我知道有很多关于此的问题,我确保已经尝试了每一个答案。我对WPF没有太多经验,我的团队也没有其他人,所以我确信我犯了一个愚蠢的错误,看不到它。我一直在为我接管的这个项目添加一个新功能,并且已经将意大利面条代码转换为MVVM解决方案的开头。我有一个模型,它源自XML文档的DeSerialization和ViewModel。它们目前都有UserControl所需的数据。所以我将DataContext设置为Code Behind。在此,MsClassification最初设置为“UN”。我将组合框更改为其他内容,并使用按钮重新读取XML,将MsClassification设置回“UN”,但View不会更新。有趣的是,如果我将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));
}
答案 0 :(得分:0)
确保在ViewModel中实现INotifyPropertyChanged,并将绑定设置为TwoWay
{Binding Path=ClassificationLevels, Mode=TwoWay}
还有一件事,...尝试使用Fody。它将为您节省大量时间 尝试使用WPF Inspector。它为您提供了更多调试绑定和更新的选项。您可以更详细地检查应用程序。