我使用Microsoft功能区。我尝试将QuickAccessButtons绑定到我的ViewModel的属性,在应用程序关闭之前保存按钮,并在下次启动后加载。
我的问题是,如果我尝试使用RibbonButtons的Contextmenu向QuickAccessToolbar添加一个新Button,我会收到错误消息Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
。 Visual Studio的调试器在内容为Source Not Available
的选项卡The call stack contains only external code
中停止。
如果我使用ItemsControl.ItemsSource,则没有错误,但绑定也不起作用。
如果我使用直接的ItemsSource-Binding并在绑定的ObservableCollection中设置一些RibbonButtons作为默认值,则会显示它们。所以这是正确的财产。
我的XAML代码如下所示:
<Ribbon Name="ribMain" SelectedIndex="0" Grid.Row="0" IsMinimized="{Binding IsRibbonMinimized}">
<!-- The Quick-Buttons in the Titlebar of the Window -->
<Ribbon.QuickAccessToolBar>
<RibbonQuickAccessToolBar ItemsSource="{Binding QuickAccessButtons, Mode=TwoWay}">
<!-- Quick-Access-Buttons here-->
</RibbonQuickAccessToolBar>
</Ribbon.QuickAccessToolBar>
<!-- ... -->
</Ribbon>
使用
设置DataContext<RibbonWindow.DataContext>
<local:ViewModelMain />
</RibbonWindow.DataContext>
并且ViewModeMain中的Property以这样的方式定义:
private ObservableCollection<RibbonButton> _QuickAccessButtons = new ObservableCollection<RibbonButton>(){new RibbonButton()};
public ObservableCollection<RibbonButton> QuickAccessButtons
{
get { return _QuickAccessButtons; }
set
{
_QuickAccessButtons = value;
RaisePropertyChanged("QuickAccessButtons");
}
}
任何人都知道我能做什么?