从用户控件中的 xaml 的依赖属性为我的视图模型分配值
我需要捕获按键事件以从自定义 DataGrid `public class ExtendedDataGrid : DataGrid {} 获取键值,并让它使用按下的键在我的视图模型中触发一个事件。准确地说,我想检查是否有人按下了删除键,然后从当前选定的记录中删除一条记录,而不使用后面的代码,因为我的所有逻辑都在我的视图模型中。
我的 ExtendedDataGrid 控件是新的,我创建了一个 GridColumns 集合(我自己的类定义),它设置视图模型中的列,并在视图模型将值发送到 DependencyProperty 的情况下工作。但是,这次我想从 PreviewKeyDown 捕获键盘输入并将其发送到视图模型。
我在 ExtendedDataGrid 类中创建了一个方法,并将其放置在构造函数中: PreviewKeyDown += OnKeyDown
;这将按预期触发事件。我创建了一个 DependencyProperty :
public static readonly DependencyProperty KeyDownValueProperty =
DependencyProperty.Register("KeyDownValue", typeof(Key), typeof(ExtendedDataGrid), null, null);
然后创建了一个方法:
private void OnKeyDown(object Sender, KeyEventArgs Args)
{
SetValue(KeyDownValueProperty, Args.Key);
}
然后声明了属性包装器:
[Bindable(true)]
public Key KeyDownValue
{
get
{
return (Key)GetValue(KeyDownValueProperty);
}
set
{
SetValue(KeyDownValueProperty, value);
}
}
我做的最后一件事是在 XAML 中添加 KeyDownValue="{Binding Path=KeyThatWasPressed}"。这在我的 Key 类型的视图模型中被声明为公共属性。
我希望按下该键会触发我创建的事件(它确实如此),然后将值发送到依赖项属性并让它通知我的视图模型中的更改/值,但事实并非如此。
一旦我明白了这一点,其他一切就会水到渠成,因为我学得很快。我只是在这里缺少一些基础知识。任何帮助都会很大。一旦我学到了足够的知识,我很可能会开始回答有关该主题的其他新手问题。
I need to capture a key press event to get the key value from my custom DataGrid `public class ExtendedDataGrid : DataGrid {} and have it fire an event in my view model with the Key that was pressed. To be precise, I want to check if someone pressed the Delete key and then delete a record from the currently selected without using code behind because all of my logic is in my view model.
My ExtendedDataGrid control is new and I have created a collection of GridColumns (My own class definition) that sets the columns from the view model and that works where the view model sends the value to the DependencyProperty. However, this time I want to capture the keyboard input from PreviewKeyDown and send it to the view model.
I created a method in my ExtendedDataGrid class and placed in the constructor : PreviewKeyDown += OnKeyDown
; This fires the event as expected. I created a DependencyProperty :
public static readonly DependencyProperty KeyDownValueProperty =
DependencyProperty.Register("KeyDownValue", typeof(Key), typeof(ExtendedDataGrid), null, null);
I then created a method :
private void OnKeyDown(object Sender, KeyEventArgs Args)
{
SetValue(KeyDownValueProperty, Args.Key);
}
I then declared the property wrapper:
[Bindable(true)]
public Key KeyDownValue
{
get
{
return (Key)GetValue(KeyDownValueProperty);
}
set
{
SetValue(KeyDownValueProperty, value);
}
}
The last thing I did was to add in XAML, KeyDownValue="{Binding Path=KeyThatWasPressed}". This is declared as a public property in my view model of type Key.
I was hoping that the pressing of the key would fire the event I created, which it does, and then send the value to the Dependency Property and have it notify the change/value in my view model, which it does not.
Once I get this then all else will fall into place, as I am a quick learner. I am simply missing some basics here. Any help would be greatly. Once I have learned enough I may well start answering other newbie questions on this subject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 VM 绑定到 ExtendedDataGrid 时,尝试将绑定模式设置为 TwoWay
When you bind the VM to the ExtendedDataGrid, try setting the binding mode to TwoWay