连接/结合相同命令集到多个控件

我有一个帆布内的控制,我希望能够使用箭头键来移动它。 为了尝试的东西出来,我创建了以下类,我想要做什么。

<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CanvasControlArrowKeyTest" Height="300" Width="300"> <Canvas> <Canvas.InputBindings> <KeyBinding Key="Down" Command="MoveDown" /> <KeyBinding Key="Up" Command="MoveUp" /> <KeyBinding Key="Right" Command="MoveRight" /> <KeyBinding Key="Left" Command="MoveLeft" /> </Canvas.InputBindings> <Button> <Button.CommandBindings> <CommandBinding Command="MoveDown" Executed="MoveDown_Executed" /> <CommandBinding Command="MoveUp" Executed="MoveUp_Executed" /> <CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" /> <CommandBinding Command="MoveRight" Executed="MoveRight_Executed" /> </Button.CommandBindings> </Button> </Canvas> </Window>

下面的代码隐藏的一个片段:

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e) { var uiElement = (UIElement)sender; double value = Canvas.GetTop(uiElement); value = Double.IsNaN(value) ? 0 : value; value++; Canvas.SetTop(uiElement, value < 0 ? 0 : value); }

这一切工作正常,但我真正想要的是具有这种能力,而不只是一个一堆按钮。 我怎样才能确保每一个按钮都有这些CommandBindings? 如果有一个比使用CommandBindings更简单的方法,什么可能有多大呢?

更新:根据要求,这里是另一个似乎并没有正常工作的方法:

<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CanvasControlArrowKeyTest" Height="300" Width="300"> <Window.CommandBindings> <CommandBinding Command="MoveDown" Executed="MoveDown_Executed" /> <CommandBinding Command="MoveUp" Executed="MoveUp_Executed" /> <CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" /> <CommandBinding Command="MoveRight" Executed="MoveRight_Executed" /> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Key="Down" Command="MoveDown" /> <KeyBinding Key="Up" Command="MoveUp" /> <KeyBinding Key="Right" Command="MoveRight" /> <KeyBinding Key="Left" Command="MoveLeft" /> </Window.InputBindings> <Canvas > <Button Width="50" Height="50" /> </Canvas> </Window>

C#

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e) { var uiElement = (UIElement)e.OriginalSource; // Still doesn't point to the Button double value = Canvas.GetTop(uiElement); value = Double.IsNaN(value) ? 0 : value; value++; Canvas.SetTop(uiElement, value < 0 ? 0 : value); }

更新:我放弃了这种做法。 最后我用的问题,一个不使用命令的不同的解决方案。

--------------解决方案-------------

你应该看看路由事件处理程序:http://msdn.microsoft.com/en-us/library/ms742806.aspx

上面的链接有如何做你问一个例子。

<Border Height="50" Width="300" BorderBrush="Gray" BorderThickness="1">
<StackPanel Background="LightGray" Orientation="Horizontal" Button.Click="CommonClickHandler">
<Button Name="YesButton" Width="Auto" >Yes</Button>
<Button Name="NoButton" Width="Auto" >No</Button>
<Button Name="CancelButton" Width="Auto" >Cancel</Button>
</StackPanel>
</Border>

和代码

private void CommonClickHandler(object sender, RoutedEventArgs e)
{
FrameworkElement feSource = e.Source as FrameworkElement;
switch (feSource.Name)
{
case "YesButton":
// do something here ...
break;
case "NoButton":
// do something ...
break;
case "CancelButton":
// do something ...
break;
}
e.Handled=true;
}

如果您在顶级对象(窗口在这种情况下)化CommandBindings,你可以重用他们的窗口的任何子女。

<Window x:Class="TesterApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:TesterApp"
x:Name="TheMainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="A" Command="MoveDown" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="MoveDown" Executed="MoveDown_Executed" />
</Window.CommandBindings>
<Grid>
<Button Height="30" Width="80" Content="Click" Command="MoveDown" />
</Grid>
</Window>

分类:。净 时间:2015-03-15 人气:0
本文关键词: .NET,WPF,结合
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.246 (s). 10 q(s)