2013-05-27 35 views
3

我有一個帶狀組合框(MS絲帶開源項目,.NET 4.0),這是綁定到我的視圖模型的屬性這樣的數據:RibbonComboBox不更新的SelectedValue

XAML:

<ribbon:RibbonComboBox SelectionBoxWidth="130" Margin="3,0"> 
    <ribbon:RibbonGallery 
    SelectedValue="{Binding Source={StaticResource ViewModel}, 
    Path=Document, Converter={StaticResource DocumentToDocumentNameConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
     <ribbon:RibbonGalleryCategory 
     ItemsSource="{Binding Source={StaticResource ViewModel}, 
     Path=Documents, Converter={StaticResource DocumentToDocumentNamesConverter}}"> 
     </ribbon:RibbonGalleryCategory> 
    </ribbon:RibbonGallery> 
    </ribbon:RibbonComboBox> 

視圖模型

public ViewModel { 

    #region Fields 
    private TestDocument _Document; 
    #endregion 

    #region Properties 
    public TestDocument Document 
    { 
     get 
     { 
      return ModelClass.SelectedDocument; 
     } 
     set 
     { 
      if (value != null && value != _Document) 
      { 
       _Document = value; 
       OnPropertyChanged("Document"); 
      } 
     } 
    } 
    #endregion 
} 

如果我選擇在C另一個值這工作好, omboBox輸入轉換器並顯示值 。

但如果我設置屬性在視圖模型這樣

Document = new TestDocument("DocumentName"); 

所選擇的組合框不顯示姓名。

你有什麼建議嗎?我甚至試圖綁定SelectedItem而不是SelectedValue,但這並不能解決問題。我忘了什麼嗎?

回答

1

問題是您的SelectedItem /值不是ItemSourceRibbonComboBox的一部分。所以它在設置時沒有任何影響。

您需要的是首先將新項目添加到ObservableCollection<TestDocument> Documents,然後設置Document

類似:

Documents.Add(new TestDocument("DocumentName")); 
Document = Documents[Documents.Count - 1]; 

var newDocument = new TestDocument("DocumentName"); 
Documents.Add(newDocument); 
Document = newDocument; 
+0

這是問題。謝謝,我知道我已經忘記了一些東西。 –

相關問題