WPF:在DataTemplate中处理MouseDown事件
我有以下数据模板:WPF:在DataTemplate中处理MouseDown事件,wpf,mvvm,binding,datatemplate,Wpf,Mvvm,Binding,Datatemplate,我有以下数据模板: <DataTemplate x:Key="demoDataTemplate" DataType="models:SomeClass"> <TextBlock Text="{Binding Path=SomeTextProperty}" MouseDown="{Binding HOWTO????}" /> </DataTemplate> 我在
<DataTemplate x:Key="demoDataTemplate" DataType="models:SomeClass">
<TextBlock Text="{Binding Path=SomeTextProperty}" MouseDown="{Binding HOWTO????}" />
</DataTemplate>
我在ListView中使用此DataTemplate。如何将MouseDown事件绑定到ViewModel中分配给ListView的DataContext的方法?创建处理鼠标单击的附加属性,如下所示
public class MouseClick
{
public static readonly DependencyProperty MouseLeftClickProperty =
DependencyProperty.RegisterAttached("MouseLeftClick", typeof(ICommand), typeof(MouseClick),
new FrameworkPropertyMetadata(CallBack));
public static void SetMouseLeftClick(DependencyObject sender, ICommand value)
{
sender.SetValue(MouseLeftClickProperty, value);
}
public static ICommand GetMouseLeftClick(DependencyObject sender)
{
return sender.GetValue(MouseLeftClickProperty) as ICommand;
}
public static readonly DependencyProperty MouseEventParameterProperty =
DependencyProperty.RegisterAttached(
"MouseEventParameter",
typeof(object),
typeof(MouseClick),
new FrameworkPropertyMetadata((object)null, null));
public static object GetMouseEventParameter(DependencyObject d)
{
return d.GetValue(MouseEventParameterProperty);
}
public static void SetMouseEventParameter(DependencyObject d, object value)
{
d.SetValue(MouseEventParameterProperty, value);
}
private static void CallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null)
{
UIElement element = sender as UIElement;
if (element != null)
{
if (e.OldValue != null)
{
element.RemoveHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Handler));
}
if (e.NewValue != null)
{
element.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Handler), true);
}
}
}
}
private static void Handler(object sender, EventArgs e)
{
UIElement element = sender as UIElement;
if (sender != null)
{
ICommand cmd = element.GetValue(MouseLeftClickProperty) as ICommand;
if (cmd != null)
{
RoutedCommand routedCmd =cmd as RoutedCommand;
object paramenter = element.GetValue(MouseEventParameterProperty);
if (paramenter == null)
{
paramenter = element;
}
if (routedCmd != null)
{
if (routedCmd.CanExecute(paramenter, element))
{
routedCmd.Execute(paramenter, element);
}
}
else
{
if (cmd.CanExecute(paramenter))
{
cmd.Execute(paramenter);
}
}
}
}
}
}
在您的Xmal中,更改如下
<DataTemplate x:Key="demoDataTemplate" DataType="models:SomeClass">
<TextBlock Text="{Binding Path=SomeTextProperty}" local:MouseClick.MouseLeftClick="{Binding Click}"/>
</DataTemplate>
其中local是定义MouseClick类的名称空间,Click是ViewModel中的ICommand属性如何定义您的
ViewModel
?它是在StaticResources
中还是设置在code-behind中?您使用的是code-behind还是MVVM
(model,view,viewmodel)?我使用的是MVVM。无论如何,我无法对MouseDown事件使用CodeBehind,因为我想在不同的ListView上重用DataTemplate。但我使用代码隐藏来分配viewmodel—类似listview.DataContext=new MyViewModel()@user1130329在这种情况下,我建议使用命令
。这是一个让你开始的方法!您可以从文章中下载源代码(单击下载选项卡)。我喜欢使用自定义的AttachedCommandBehavior
,它允许您将大多数事件绑定到DataContext
中的命令。您可以在中查看此示例代码。在您的情况下,不要忘记设置绑定到列表视图的ElementName
或RelativeSource
,因为它的DataContext
包含您的命令而不是单个项