2016-02-19 55 views
0

我知道有很多这方面的问题,我已经确保每一个答案已经尝试。我对WPF没有太多经验,我的团队中也没有其他人,所以我确信我犯了一个愚蠢的错误,并且看不到它。我已经为这个项目添加了一个新功能,并且已经将意大利面代码转换为MVVM解决方案的开始。我有一个模型,它来源于XML文档的DeSerialization和ViewModel。他们目前都有我需要用户控件的数据。所以我将DataContext设置为Code Behind。在此MsClassification最初设置为“UN”。我将组合框更改为其他内容,然后使用按钮重新读取将MsClassification设置回“UN”的XML,但View不会更新。有趣的是,如果我将MsLoggingLevel设置为其他值,并重新读取XML,它将更新这两个值。WPF绑定不更新为一个属性

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

确保在视图模型INotifyPropertyChanged的实施,并设置为双向

{Binding Path=ClassificationLevels, Mode=TwoWay} 

另外一件事的结合,...尝试使用Fody。它将为您节省大量时间 尝试使用WPF Inspector。它为您提供了更多的选项来调试绑定和更新。您可以更详细地检查应用程序。

+0

Fody绝对看起来像一个不错的工具。从来没有听说过,但看起来真的可以节省大量的时间。 – mcy

+0

我会看看Fody。 ClassifcationLevels需要设置一次,然后完成。 ClassificationLevel应该是两种方式,所以我补充说。不用找了。我确实实施了INotifyPropertyChanged。关于这个问题的一个问题,以及在这方面的一个noob问题。我必须在所有类中实现INotifyPropertyChanged(VSParentReference.InitRules) –

+0

是的。每个需要绑定到控件的类都需要实现它。我一般的经验法则是让每个虚拟机实现它,只有虚拟机被绑定。 福迪将为您节省大量的时间。 – user853710