Wou*_*ter 4 c# wpf xaml binding
如何启用鼠标绑定到右键的释放?目前我在xaml中有以下代码,它与关闭wpf窗口有关.这里的问题是,因为它在关闭窗口时对点击的加速作出反应,它激活桌面上的上下文菜单.
<MouseBinding Command="Close" MouseAction="RightClick" />
Run Code Online (Sandbox Code Playgroud)
在MouseBinding
不支持鼠标后续行动,只按下鼠标的动作,所以只需你想用做什么不能做MouseBinding
.最简单的替代方法是MouseRightButtonUp
在您将其添加MouseBinding
为InputBinding
to 的同一元素上的事件的代码隐藏事件处理程序.但我怀疑你是出于自己的原因而避开事件处理程序的方法,但你应该澄清这是否是你的意图.
可用的剩余选项是某种形式的附加行为.有很多方法可以做到这一点,但我将使用相当标准System.Windows.Interactivity
的Blend行为.您所要做的就是为鼠标右键添加事件触发器并调用close命令.您需要执行此操作的所有内容都在SDK中,但遗憾的是,调用命令的InvokeCommandAction
功能不能正确支持路由命令,因此我编写了一个名为的替代方法ExecuteCommand
.
这是一些示例标记:
<Grid Background="White">
<Grid.CommandBindings>
<CommandBinding Command="Close" Executed="CommandBinding_Executed"/>
</Grid.CommandBindings>
<!--<Grid.InputBindings>
<MouseBinding Command="Close" MouseAction="RightClick"/>
</Grid.InputBindings>-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonUp">
<utils:ExecuteCommand Command="Close"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel>
<TextBox Text="Some Text"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
您的旧方法已被注释掉,新方法位于其下方.
以下是连接路由命令的代码隐藏:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
Run Code Online (Sandbox Code Playgroud)
最后,这里是执行ExecuteCommand
:
public class ExecuteCommand : TriggerAction<DependencyObject>
{
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommand), null);
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommand), null);
public UIElement CommandTarget
{
get { return (UIElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(ExecuteCommand), null);
protected override void Invoke(object parameter)
{
if (Command is RoutedCommand)
{
var routedCommand = Command as RoutedCommand;
var commandTarget = CommandTarget ?? AssociatedObject as UIElement;
if (routedCommand.CanExecute(CommandParameter, commandTarget))
routedCommand.Execute(CommandParameter, commandTarget);
}
else
{
if (Command.CanExecute(CommandParameter))
Command.Execute(CommandParameter);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你没有使用路由命令但是使用了说MVVM RelayCommand,你可以不需要ExecuteCommand
,而是可以使用InvokeCommandAction
.
此示例使用行为.如果您不熟悉行为,请安装Expression Blend 4 SDK并添加此命名空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Run Code Online (Sandbox Code Playgroud)
并添加System.Windows.Interactivity
到您的项目.
归档时间: |
|
查看次数: |
8377 次 |
最近记录: |