gpt4 book ai didi

c# - 在一个窗口中定义的 WPF CommandBindings 在另一个窗口中不可用

转载 作者:行者123 更新时间:2023-11-30 21:11:43 24 4
gpt4 key购买 nike

我遇到了这个问题,我在 MainWindow 中定义了所有这些 CommandBindings,并且所述命令在该窗口中可用,可用于任何 Button 或 MenuItem。问题是,如果在其他窗口中命令绑定(bind)不可用(它始终为 false),即使新窗口的所有者是 MainWindow。

You can see the problem here.

非常感谢任何帮助。

这是代码。

XAML 主窗口:

<Window x:Class="ContextMenuDialogProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ContextMenuDialogProblem"
Title="MainWindow" Height="350" Width="525"
FocusManager.FocusedElement="{Binding RelativeSource={x:Static RelativeSource.Self}, Mode=OneTime}">
<Window.CommandBindings>
<CommandBinding Command="local:LocalCommandManager.ShowDialogCommand" CanExecute="CanExecuteShowDialogCommand" Executed="ShowDialogCommandExecuted" />
</Window.CommandBindings>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Command="local:LocalCommandManager.ShowDialogCommand" />
</ContextMenu>
</Window.ContextMenu>
<Grid Background="Red">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0"
Content="Open SubWindow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="6"
Click="Button_Click" />
<Button Grid.Row="1"
Content="Show Dialog Command Test"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="6"
Command="local:LocalCommandManager.ShowDialogCommand" />
</Grid>
</Window>

CS 主窗口:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void CanExecuteShowDialogCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}

private void ShowDialogCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Show Dialog");
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Window wnd = new SubWindow() { Owner = this };
wnd.Show();
}
}

CS LocalCommandManager:

public static class LocalCommandManager
{
private static object syncRoot = new object();

private static RoutedUICommand _showDialogCommand;
public static RoutedUICommand ShowDialogCommand
{
get
{
lock (syncRoot)
{
if (_showDialogCommand == null)
_showDialogCommand = new RoutedUICommand("Show Dialog", "ShowDialogCommand", typeof(LocalCommandManager));
return _showDialogCommand;
}
}
}
}

XAML 子窗口:

<Window x:Class="ContextMenuDialogProblem.SubWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:ContextMenuDialogProblem"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SubWindow" Height="300" Width="300">
<Grid>
<Button Command="local:LocalCommandManager.ShowDialogCommand" Content="Show Dialog" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="6" />
</Grid>
</Window>

最佳答案

CommandBindings 的范围仅限于定义它的元素,因此这种行为是完全正常的。如果你想在那里使用它,你必须将 CommandBinding 添加到 SubWindow

关于c# - 在一个窗口中定义的 WPF CommandBindings 在另一个窗口中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com