如果我为RibbonLibrary添加了绑定集合,如何添加快速访问项容器默认值。它的抛出当ItemSource正在使用时,操作无效,而我是从UI添加快速访问工具项。
<r:Ribbon Name="ribbon">
<r:Ribbon.QuickAccessToolBar>
<r:RibbonQuickAccessToolBar ItemsSource ="{Binding QuickMenuItems, Mode=OneWay}">
<r:RibbonQuickAccessToolBar.ItemTemplate>
<DataTemplate>
<StackPanel>
<r:RibbonButton QuickAccessToolBarId="{Binding RibbonId}" Label="{Binding Label}" SmallImageSource="{Binding ImageUri}" Command="{Binding Command}"/>
</StackPanel>
</DataTemplate>
</r:RibbonQuickAccessToolBar.ItemTemplate>
</r:RibbonQuickAccessToolBar>
</r:Ribbon.QuickAccessToolBar>
<r:RibbonTab Header="Home">
<r:RibbonGroup x:Name="Clipboard" ItemsSource ="{Binding MenuItems, Mode=OneWay}" >
<r:RibbonGroup.ItemTemplate>
<DataTemplate>
<StackPanel>
<r:RibbonButton QuickAccessToolBarId="{Binding RibbonId}" Label="{Binding Label}" SmallImageSource="{Binding ImageUri}" Command="{Binding Command}"/>
</StackPanel>
</DataTemplate>
</r:RibbonGroup.ItemTemplate>
</r:RibbonGroup>
</r:RibbonTab>
</r:Ribbon>
ObservableCollection<RibbonItem> _MenuItems;
ObservableCollection<RibbonItem> _QuickMenuItems;
public ObservableCollection<RibbonItem> MenuItems
{
get { return _MenuItems; }
}
public ObservableCollection<RibbonItem> QuickMenuItems
{
get { return _QuickMenuItems; }
}
public class RibbonItem
{
public RibbonItem(string label, string imageUri, ICommand command, string ribbonId)
{
Label = label;
ImageUri = imageUri;
Command = command;
}
public string Label { get; private set; }
public string ImageUri { get; private set; }
public ICommand Command { get; private set; }
public string RibbonId { get; private set; }
}
时出错
答案 0 :(得分:4)
问题是ContextMenu正在尝试使用ItemCollection.Add()将项添加到集合中,但如果使用ItemsSource来填充集合而该方法将抛出异常,则不支持此方法。 / p>
请参阅源代码:ItemCollection.cs
一种解决方案是隐藏ContextMenu以避免调用不支持的函数,或尝试将其替换为您自己的自定义ContextMenu,后者绑定到ViewModel上的命令并更新QuickMenuItem。
可能存在使用附加属性的某种黑客攻击。
答案 1 :(得分:1)
一种解决方法是不使用RibbonQuickAccessToolBar.ItemsSource。如果需要通知QuickAccessToolbar的更改,请订阅RibbonQuickAccessToolBar.Items的INotifyCollectionChanged。
public RibbonWindow() {
InitializeComponent();
((INotifyCollectionChanged)QuickAccessToolBar.Items).CollectionChanged += QuickAccessToolBar_ItemsCollectionChanged;
}
private void QuickAccessToolBar_ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
switch (e.Action) {
//Do stuff or synchronize to observableCollection
}
}
答案 2 :(得分:1)
这将允许您从功能区控件和ViewModel添加快捷菜单项。我使用ListBox
作为ViewModel和RibbonQuickAccessToolBar
之间的代理。将项目添加到ListBox
后,视图会将其添加到RibbonQuickAccessToolBar
。
创建一个折叠的ListBox
,它将绑定到ViewModel中的集合,并删除ItemsSource
的{{1}}绑定:
RibbonQuickAccessToolBar
在代码隐藏中使用ListBox的DataContextChanged来附加ListBox.ItemsSource的CollectionChanged事件的事件处理程序:
<ListBox ItemsSource="{Binding QuickMenuItems, Mode=OneWay}"
x:Name="ProxyListBox"
Visibility="Collapsed"/>
<ribbon:Ribbon Name="ribbon">
<ribbon:Ribbon.QuickAccessToolBar>
<ribbon:RibbonQuickAccessToolBar x:Name="QuickAccessToolBar" DataContextChanged="QuickAccessToolBar_OnDataContextChanged">
<ribbon:RibbonQuickAccessToolBar.ItemTemplate>
<DataTemplate>
<StackPanel>
<ribbon:RibbonButton QuickAccessToolBarId="{Binding RibbonId}" Label="{Binding Label}" SmallImageSource="{Binding ImageUri}" Command="{Binding Command}"/>
</StackPanel>
</DataTemplate>
</ribbon:RibbonQuickAccessToolBar.ItemTemplate>
</ribbon:RibbonQuickAccessToolBar>
</ribbon:Ribbon.QuickAccessToolBar>
<ribbon:RibbonTab Header="Home">
<ribbon:RibbonGroup x:Name="Clipboard" ItemsSource ="{Binding MenuItems, Mode=OneWay}" >
<ribbon:RibbonGroup.ItemTemplate>
<DataTemplate>
<StackPanel>
<ribbon:RibbonButton QuickAccessToolBarId="{Binding RibbonId}" Label="{Binding Label}" SmallImageSource="{Binding ImageUri}" Command="{Binding Command}"/>
</StackPanel>
</DataTemplate>
</ribbon:RibbonGroup.ItemTemplate>
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>
ViewModel与之前相同:
private void ProxyListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var newItem in e.NewItems)
{
QuickAccessToolBar.Items.Add(newItem);
}
}
if (e.OldItems != null)
{
foreach (var oldItem in e.OldItems)
{
QuickAccessToolBar.Items.Remove(oldItem);
}
}
}
private void QuickAccessToolBar_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
((INotifyCollectionChanged)ProxyListBox.Items).CollectionChanged += ProxyListBox_CollectionChanged;
}
答案 3 :(得分:0)
最后,我能够通过代码来解决我的问题,因为我正在使用RibbonWindow
的附加行为,这是解决方案:
public class RibbonWindowQuickAccessItemBehaviour : Behavior<RibbonWindow>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += RibbonControl_Loaded;
AssociatedObject.Closing += RibbonControl_Closing;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= RibbonControl_Loaded;
AssociatedObject.Closing -= RibbonControl_Closing;
}
private void RibbonControl_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
var ribbonWindow = (RibbonWindow)sender;
var ribbon = ribbonWindow.FindAllChildrenOfType<Microsoft.Windows.Controls.Ribbon.Ribbon>().Where(a => a.Name == "ribbon").FirstOrDefault();
if (ribbon == null) return;
var dataContext = ribbon.DataContext;
if (dataContext is IRibbonMainWindowViewModel)
{
IRibbon windowsRibbon = (dataContext as IRibbonMainWindowViewModel).WindowRibbon;
windowsRibbon.QuickAccessMenuItems.Clear();
if (ribbon.QuickAccessToolBar != null && ribbon.QuickAccessToolBar.Items != null && ribbon.QuickAccessToolBar.Items.Count > 0)
{
foreach (var item in ribbon.QuickAccessToolBar.Items)
{
if (item is RibbonButton)
{
var ribbonItem = item as RibbonButton;
windowsRibbon.QuickAccessMenuItems.Add
(new Gno.Framework.ClientShell.Ribbon.MenuItem(ribbonItem.Label,
ribbonItem.SmallImageSource == null ? string.Empty : ribbonItem.SmallImageSource.ToString(),
ribbonItem.Command));
}
}
}
}
}
private void RibbonControl_Loaded(object sender, RoutedEventArgs e)
{
var ribbonWindow = (RibbonWindow)sender;
var ribbon = ribbonWindow.FindAllChildrenOfType<Microsoft.Windows.Controls.Ribbon.Ribbon>().Where(a => a.Name == "ribbon").FirstOrDefault();
if (ribbon == null) return;
var dataContext = ribbon.DataContext;
if (dataContext is IRibbonMainWindowViewModel)
{
IRibbon windowsRibbon = (dataContext as IRibbonMainWindowViewModel).WindowRibbon;
var quickAccessMenus = windowsRibbon.QuickAccessMenuItems;
if (quickAccessMenus != null && quickAccessMenus.Count > 0)
{
ribbon.QuickAccessToolBar = new RibbonQuickAccessToolBar();
foreach (var quickMenu in windowsRibbon.QuickAccessMenuItems)
{
var ribbonButton = new RibbonButton();
ribbonButton.Command = quickMenu.Command;
ribbonButton.Label = quickMenu.Header;
if (!string.IsNullOrEmpty(quickMenu.ImageUri))
ribbonButton.SmallImageSource = new BitmapImage(new Uri(quickMenu.ImageUri));
ribbon.QuickAccessToolBar.Items.Add(ribbonButton);
}
}
}
}
}
实际上这是保存QuickAccessToolBarItem的代码,因此Window Close和Load事件需要绑定。