How can I add Quick Access Item container default by RibbonLibrary if I have binded collection for it. Its throws Operation is not valid while ItemSource is in use while is I add Quick Access tool item from 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; }
}
Error while
Add comment if not clear.
The problem is that the ContextMenu is trying to add items to the collection using ItemCollection.Add(), but this method is not supported if an ItemsSource is being used to populate the collection instead and the method will throw an exception.
See the source code: ItemCollection.cs
One solution would be to hide the ContextMenu to avoid its unsupported functions being called, or try to replace it with your own custom ContextMenu, which binds to a command on the ViewModel and updates the QuickMenuItems.
There may be some kind of hack using attached properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With