2016-02-19 23 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