RibbonComboBox不会更新SelectedValue

时间:2013-05-27 12:00:16

标签: c# wpf mvvm

我有一个功能区组合框(MS Ribbon OpenSource项目,.Net 4.0),它是绑定到我的viewmodel属性的数据,如下所示:

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
}

这很好,如果我在ComboBox中选择转换器输入的另一个值和值 所示。

但是如果我在ViewModel中像这样设置属性

Document = new TestDocument("DocumentName");

ComboBox未显示所选名称。

你有什么建议吗?我甚至尝试绑定SelectedItem而不是SelectedValue,但这并没有解决问题。我忘记了什么吗?

1 个答案:

答案 0 :(得分:1)

问题是您的SelectedItem /值不属于ItemSource RibbonComboBox的一部分。所以它在设置时没有任何效果。

您需要首先将新项目添加到ObservableCollection<TestDocument> Documents,然后设置Document

类似的东西:

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

var newDocument = new TestDocument("DocumentName");
Documents.Add(newDocument);
Document = newDocument;
相关问题