[英]Wpf Dependency Property Dynamic Key Down
I don't know if my title is proper.我不知道我的标题是否合适。
But what i am doing here is I am creating a Program that draw rectangle on canvas.但是我在这里做的是我正在创建一个在画布上绘制矩形的程序。
As of now i can draw on canvas by triggering a dependency property when i press the SHIFT
key on keyboard.到目前为止,当我按下键盘上的
SHIFT
键时,我可以通过触发依赖属性在画布上进行绘制。 But i want it to become dynamic.但我希望它变得动态。 I create another Dependency Property then on my XAML i can pass the KEY
我创建了另一个依赖属性,然后在我的 XAML 上我可以传递 KEY
something like this on my XAML在我的 XAML 上是这样的
DrawingTrigger="Ctrl+Shift"
As of now this is my class到目前为止,这是我的班级
class WindowHelper : Behavior<Window>
{
public bool IsDrawing
{
get { return (bool)GetValue(IsDrawingProperty); }
set { SetValue(IsDrawingProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDrawing. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDrawingProperty =
DependencyProperty.Register("IsDrawing", typeof(bool), typeof(WindowHelper),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null));
protected override void OnAttached()
{
AssociatedObject.KeyDown += AssociatedObject_KeyDown;
AssociatedObject.KeyUp += AssociatedObject_KeyUp;
base.OnAttached();
}
private void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
IsDrawing = false;
}
private void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
IsDrawing = true;
}
protected override void OnDetaching()
{
AssociatedObject.KeyDown -= AssociatedObject_KeyDown;
AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
base.OnDetaching();
}
}
And upon searching i found a code that print the Keyboard Modifiers pressed在搜索时,我发现了一个打印键盘修饰符的代码
Console.WriteLine(Keyboard.Modifiers);
With the Keyboard Modifier.使用键盘修改器。 When i pressed the Ctrl+Shift+Alt
当我按下 Ctrl+Shift+Alt
it gives me它给了我
Alt, Control, Shift
With this how can i use this to solve my problem?有了这个,我该如何使用它来解决我的问题?
Is it a good practice to check if each word in the DrawingTrigger
is found the set the IsDrawing
into True
or any suggestion.它是一个很好的做法,以检查是否在每个字
DrawingTrigger
找到了一套IsDrawing
成True
或任何建议。
Key and Modifiers are two different things.键和修饰符是两个不同的东西。 I'd just add / remove a KeyBinding object on the Window.InputBindings.
我只是在 Window.InputBindings 上添加/删除一个 KeyBinding 对象。 It'll handle 99% of your work for you.
它将为您处理 99% 的工作。 It already supports combining modifiers with + and handling left and right keys, etc. You just define pass Ctrl+Shift into the Modifers for example and give it an ICommand to call back into.
它已经支持将修饰符与 + 组合并处理左右键等。例如,您只需定义传递 Ctrl+Shift 到修饰符中,并为其提供一个 ICommand 以回调。 Easy peazy.
容易豌豆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.