lev*_*ovd 10 wpf datatemplate event-handling
我有一个数据模板中的WPF ComboBox(列表框中的很多组合框),我想处理输入按钮.如果它是一个按钮会很容易 - 我会使用Command + Relative绑定路径等.不幸的是,我不知道如何使用Command处理按键或如何从模板设置事件处理程序.有什么建议?
ezo*_*tko 14
您可以在设置模板的样式中使用EventSetter:
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseWheel" Handler="GroupListBox_MouseWheel" />
<Setter Property="Template" ... />
</Style>
Run Code Online (Sandbox Code Playgroud)
我通过使用常用的事件处理程序解决了我的问题,在该处理程序中,我遍历可视化树,找到相应的按钮并调用它的命令。如果其他人也有同样的问题,请发表评论,我将提供更多实现细节。
UPD
这是我的解决方案:
我在可视化树中搜索按钮,然后执行与按钮关联的命令。
查看.xaml:
<ComboBox KeyDown="ComboBox_KeyDown"/>
<Button Command="{Binding AddResourceCommand}"/>
Run Code Online (Sandbox Code Playgroud)
查看.xaml.cs:
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var parent = VisualTreeHelper.GetParent((DependencyObject)sender);
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i) as Button;
if (null != child)
{
child.Command.Execute(null);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20552 次 |
| 最近记录: |