thu*_*eys 2 wpf xaml listview wpf-controls listviewitem
我有一个GridView,我想在列表中的项目上检测doubleclick事件,我按如下方式执行:
<ListView>
<ListView.View >
<GridView >
<GridViewColumn Header="FileName">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding FileName}" MouseDoubleClick="Configuration_MouseDoubleClick"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding CreationDate}" Header="Date"/>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
问题是我只能通过将双击附加到模板中的控件来检测双击.
我怎样才能把这MouseDoubleClick件事附加到整体上ListViewItem?PRISM有没有解决方案?
Fre*_*lad 12
您可以像这样在ItemContainerStyle中将MouseDoubleClick事件添加到ListViewItem
<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Run Code Online (Sandbox Code Playgroud)
代码背后..
void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
dlf*_*dlf 10
如果您正在使用MVVM,您可以通过常规方式弥补代码隐藏和视图模型之间的差距 - 使用附加行为:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
public sealed class HandleDoubleClickBehavior
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
"Command", typeof (ICommand), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(ICommand), OnComandChanged));
public static void SetCommand(DependencyObject element, ICommand value)
{
element.SetValue(CommandProperty, value);
}
public static ICommand GetCommand(DependencyObject element)
{
return (ICommand) element.GetValue(CommandProperty);
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
"CommandParameter", typeof (object), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(object)));
public static void SetCommandParameter(DependencyObject element, object value)
{
element.SetValue(CommandParameterProperty, value);
}
public static object GetCommandParameter(DependencyObject element)
{
return (object) element.GetValue(CommandParameterProperty);
}
private static void OnComandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var c = d as Control;
if (c == null)
throw new InvalidOperationException($"can only be attached to {nameof(Control)}");
c.MouseDoubleClick -= OnDoubleClick;
if (GetCommand(c) != null)
c.MouseDoubleClick += OnDoubleClick;
}
private static void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
var d = sender as DependencyObject;
if (d == null)
return;
var command = GetCommand(d);
if (command == null)
return;
var parameter = GetCommandParameter(d);
if (!command.CanExecute(parameter))
return;
command.Execute(parameter);
}
}
Run Code Online (Sandbox Code Playgroud)
与你的工具箱中,你可以写XAML像这样(假设PersonViewModel包含字符串性质Name和Title,和ICommand命名属性SayHiCommand期望一个字符串参数):
<ListView ItemsSource="{Binding Persons}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="local:HandleDoubleClickBehavior.Command" Value="{Binding SayHiCommand}" />
<Setter Property="local:HandleDoubleClickBehavior.CommandParameter" Value="{Binding Name}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" />
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6720 次 |
| 最近记录: |