我的问题基本上是this one。尽管如此,我认为这将有助于提供更多的信息和代码,使其更容易重现问题。如何绑定RibbonComboBox SelectedItem
使用来自RibbonControlsLibrary的Microsoft.Windows.Controls.Ribbon.RibbonComboBox感觉就像是漫步在一个充满bug的大沼泽中,而不是你如果知道解决方法的话。
Anywho。我遇到的最大问题是数据绑定我的SelectedItem。
以下是我开始使用(在我找到约RibbonGallery?)。要在ComboBox的子元素上使用ItemsSource和SelectedItem,即使在同一级上也已经给了我heebie-jeebies,但这似乎是正确的。
在示例应用程序中,我将ViewItem设置为ViewModel的构造函数中。但是,运行应用程序时,不会显示SelectedItem。即使VS设计师正确地显示“第二选择”!
运行的应用程序: VS设计师:
在调试的SelectedItem二传手,你会发现多遍。在第一次在ctor中设置“第二个选项”(1,见下面的调试日志)后,它将重置为空(2)(通过外部代码,我认为控制本身)。当在UI中打开下拉菜单时,它将再次设置为空(3),然后在选择一个值时,将该值设为两倍(4,5)。我选择了“第二选项”,然后用“第一选项”(6-9)重复该过程。这产生了以下日志(忽略从色带控制......在1001个绑定例外):
的一个大问题是明显(2),这是我的重置初始选择。看起来像第一次显示控件时,它被重置。一个非常丑陋的解决方法是通过定时器设置值。在用户控件的Loaded事件中设置它在这个示例应用程序中适用于我,但在我较重的实际应用程序中却没有。无论如何,所有这些感觉都是错误的。有谁知道更好的解决方案?
的XAML:
<UserControl x:Class="WpfApplication1.RibbonComboBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:r="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<local:ViewModel />
</UserControl.DataContext>
<Grid>
<r:Ribbon >
<r:RibbonTab Header="First Tab">
<r:RibbonGroup Header="Group">
<r:RibbonComboBox >
<r:RibbonGallery SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<r:RibbonGalleryCategory ItemsSource="{Binding Controls}" DisplayMemberPath="Caption" />
</r:RibbonGallery>
</r:RibbonComboBox>
</r:RibbonGroup>
</r:RibbonTab>
<r:RibbonTab Header="Second Tab" />
</r:Ribbon>
</Grid>
</UserControl>
视图模型:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
namespace WpfApplication1
{
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<ControlBaseModel> Controls { get; private set; }
private ControlBaseModel _selectedItem;
public ControlBaseModel SelectedItem { get { return _selectedItem; } set { LogSelectedItemChange(value); _selectedItem = value; OnPropertyChanged("SelectedItem"); } }
public ViewModel()
{
this.Controls = new ObservableCollection<ControlBaseModel>();
this.Controls.Add(new ControlBaseModel() { Caption = "first option" });
this.Controls.Add(new ControlBaseModel() { Caption = "second option" });
this.SelectedItem = this.Controls[1]; // set to second option
}
int i = 0;
private void LogSelectedItemChange(ControlBaseModel value)
{
i++;
string setObject = "null";
if (value != null)
{
setObject = value.Caption;
}
Debug.WriteLine(string.Format("{0}: SelectedItem.set(): {1}", i, setObject));
}
}
public class ControlBaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _name;
public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } }
private string _caption;
public string Caption { get { return _caption; } set { _caption = value; OnPropertyChanged("Caption"); } }
}
}
你救了我的大量时间...非常感谢 !!! – 2016-08-26 20:09:59