【问题标题】:RibbonComboBox doesn't update SelectedValueRibbonComboBox 不更新 SelectedValue
【发布时间】:2013-05-27 12:00:15
【问题描述】:

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

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 但这并不能解决问题。我是不是忘记了什么?

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    问题是您的SelectedItem / 值不是RibbonComboBoxItemSource 的一部分。所以设置的时候没有任何作用。

    您需要先将新项目添加到ObservableCollection&lt;TestDocument&gt; Documents,然后设置Document

    类似:

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

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

    【讨论】:

    • 这就是问题所在。谢谢,我知道我忘记了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    相关资源
    最近更新 更多