【发布时间】:2011-06-24 11:12:06
【问题描述】:
我正在尝试在另一个窗口中访问 MainWindow.xaml 中定义的命令。我只能得到这些命令的灰色标题。我想知道应该做什么才能获得完全访问权限。
命令示例:
public partial class MainWindow : Window
{
public static RoutedUICommand AddCommand1 = new RoutedUICommand("Command ", "command1", typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
this.CommandBindings.Add(new CommandBinding(AddCommand1, AddCommand1Executed));
}
private void AddCommand1Executed(object sender, ExecutedRoutedEventArgs e)
{
AddNewItem picker = new AddNewItem();
picker.ShowDialog();
}
我通过数据绑定以样式访问这些命令:
<Menu x:Name="TaskMenuContainer"><MenuItem x:Name="menuItem" Header="TASKS" ItemsSource="{Binding}" Template="{DynamicResource TaskMenuTopLevelHeaderTemplateKey}">
<MenuItem.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Command" Value="{Binding}" />
<Setter Property="Header" Value="{Binding Text, RelativeSource={RelativeSource Self}}" />
<Setter Property="CommandTarget" Value="{Binding RelativeSource={RelativeSource Self}}"/>
</Style>
</MenuItem.ItemContainerStyle>
这些命令在通过框架加载到 MainWindow.xaml 的页面中工作。但是,如果我有不属于 MainWindow.xaml 的弹出窗口,这些命令只会显示为灰色并且不再起作用(无法执行)。任何建议都非常感谢!
【问题讨论】:
标签: wpf wpf-controls binding