Wpf 将命令绑定到列表
我有相同的命令,我想用于对话框类型窗口上的两个控件。作为一个潜在的有趣背景,我使用了Josh Smith的ViewModel/RelayCommand思想,因为我是WPF的新手,这是我所看到的第一件我能够从全局角度真正理解的事情 因此,该命令是ViewModel的一个属性,通过按钮的内置支持,在XAML中绑定到该命令非常简单和轻松:Wpf 将命令绑定到列表,wpf,data-binding,viewmodel,Wpf,Data Binding,Viewmodel,我有相同的命令,我想用于对话框类型窗口上的两个控件。作为一个潜在的有趣背景,我使用了Josh Smith的ViewModel/RelayCommand思想,因为我是WPF的新手,这是我所看到的第一件我能够从全局角度真正理解的事情 因此,该命令是ViewModel的一个属性,通过按钮的内置支持,在XAML中绑定到该命令非常简单和轻松: <Button ... Command="{Binding Path=PickCommand}" Content="_Ok"></Button&
<Button ... Command="{Binding Path=PickCommand}" Content="_Ok"></Button>
现在在ListView中,我唯一能使用连接到双击触发的相同命令的方法是使用事件处理程序:
<ListView ...
ItemsSource="{Binding Path=AvailableProjects}"
SelectedItem="{Binding Path=SelectedProject, Mode=TwoWay}"
MouseDoubleClick="OnProjectListingMouseDoubleClick"
>
private void OnProjectListingMouseDoubleClick(object sender, MouseButtonEventArgs e) {
var vm = (ProjectSelectionViewModel) DataContext;
vm.Pick(); // execute the pick command
}
ProjectListingMousedoubleclick上的私有void(对象发送器,MouseButtonEventArgs e){
var vm=(ProjectSelectionViewModel)数据上下文;
vm.Pick();//执行Pick命令
}
有没有办法通过绑定按钮来实现这一点
干杯,贝里尔 您的SelectBehavior类非常正确,但我对您的xaml代码感到困惑。通过在listViewItem上设置“样式”,我得到了我要执行的命令所在的DataContext的子级。因此,我将该行为附加到ListView本身:
<ListView ...Style="{StaticResource _attachedPickCommand}" >
并将样式放入资源字典中:
<Style x:Key="_attachedPickCommand" TargetType="ListView">
<Setter Property="behaviors:SelectionBehavior.DoubleClickCommand" Value="{Binding Path=PickCommand}" />
</Style>
它起作用了!但设置列表视图的样式属性“感觉”很尴尬。这仅仅是因为我不喜欢wpf中的视觉风格,还是有更好的方法
干杯,谢谢贝里尔是的,有!可以使用附加的行为并将命令绑定到该行为
public class SelectionBehavior {
public static readonly DependencyProperty CommandParameterProperty=
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(SelectionBehavior));
public static readonly DependencyProperty DoubleClickCommandProperty=
DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(SelectionBehavior),
new PropertyMetadata(OnDoubleClickAttached));
private static void OnDoubleClickAttached(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var fe=(FrameworkElement)d;
if(e.NewValue!=null && e.OldValue==null) {
fe.PreviewMouseDown+=fe_MouseDown;
} else if(e.NewValue==null && e.OldValue!=null) {
fe.PreviewMouseDown-=fe_MouseDown;
}
}
private static void fe_MouseDown(object sender, MouseButtonEventArgs e) {
if(e.ClickCount==2) {
var dep=(FrameworkElement)sender;
var command=GetDoubleClickCommand(dep);
if(command!=null) {
var param=GetCommandParameter(dep);
command.Execute(param);
}
}
}
public static ICommand GetDoubleClickCommand(FrameworkElement element) {
return (ICommand)element.GetValue(DoubleClickCommandProperty);
}
public static void SetDoubleClickCommand(FrameworkElement element, ICommand value) {
element.SetValue(DoubleClickCommandProperty, value);
}
public static object GetCommandParameter(DependencyObject element) {
return element.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DependencyObject element, object value) {
element.SetValue(CommandParameterProperty, value);
}
}
在xaml中,您需要为ListViewItem设置一个样式,该样式表示ListView中的数据。范例
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="local:SelectionBehavior.DoubleClickCommand" Value="{Binding Path=DataContext.PickCommand}"/>
<Setter Property="local:SelectionBehavior.CommandParameter" Value="{Binding Path=DataContext}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
以下是有关Hi Pavel的更多信息。我现在不能把这些都弄清楚,但它是可重用的吗?如果不是的话,看起来是一个相当大的设置。乔希·史密斯也是——看起来这家伙是一个很好的人,可以和他一起呆一会儿。当然有很多材料。太棒了!Berryl,是的,它当然是可重复使用的。可以在任何FrameworkElement上设置此行为并向其附加命令。例如,可以创建图像元素并将双击行为附加到该元素。现在,只要双击图像,就会执行PickImageCommand。也可以选择以相同的方式添加命令参数。Hi Pavel。你能看看我对你的(工作)想法和评论的执行情况吗?谢谢贝里尔,我很高兴听到这对你有用。您可以直接在listview上设置附加的命令。它应该可以正常工作。如果您有多个ListView想要使用相同的属性,但您不必这样做,那么它有助于将其放入样式中。希望有帮助!