首页 > 其他分享 >WPF在ListView中绑定Command命令的写法

WPF在ListView中绑定Command命令的写法

时间:2024-05-27 22:14:19浏览次数:17  
标签:DataContext DoCommand 绑定 Binding RelativeSource Command WPF ListView

假定:
ViewModel中有一个数据源叫Persons,有一个命令叫DoCommand,通过System.Windows.Interactivity触发器绑定鼠标MouseUp事件,当UI端绑定了DataContext数据上下文之后,
Command="{Binding DoCommand}"是找不到这个命令的,必须使用Binging类的RelativeSource属性先找到当前UI,再找到DataContext里面的DoCommand才可以。如下所示

<ListView ItemsSource ="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
<GridViewColumn Header="选项">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="操作">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseUp">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:BindingWindow}},Path=DataContext.DoCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TextBlock},Path=Text}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
关键代码:
Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:BindingWindow}},Path=DataContext.DoCommand}"

其中,BindingWindow表示当前View窗体的类名。


————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq_37189288/article/details/117415423

标签:DataContext,DoCommand,绑定,Binding,RelativeSource,Command,WPF,ListView
From: https://www.cnblogs.com/webenh/p/18216654

相关文章

  • WPF之单例模式
    项目2019/10/09 问题2019年10月9日星期三上午2:461、为了实现单例模式,在App类中添加了如下代码,使用了信号量,但是为什么返回;isNew一直为truepublicpartialclassApp:Application   {       protectedoverridevoidOnStartup(StartupEventArgs......
  • WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
    1.DataGrid的使用自动显示行号(修复删除行时行号显示不正确)  ViewCodedgTool.LoadingRow+=newEventHandler<DataGridRowEventArgs>(dgTool_LoadingRow);dgTool.UnloadingRow+=newEventHandler<DataGridRowEventArgs>(dgTool_UnloadingRow);voi......
  • WebView2在WPF中的应用
    开发环境运行环境:.Net6开发环境:VisualStudio202217.1.3框架语言:WPF安装WebView2通过PackageManager控制台安装Install-PackageMicrosoft.Web.WebView2通过Nuget包管理器安装在窗体中添加名字空间:xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;asse......
  • C# wpf之控制屏幕显示方向旋转
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents......
  • 使用.Net Core开发WPF App系列教程( 八、WPF中的常用控件(下))
    使用.NetCore开发WPFApp系列教程一、.NetCore和WPF介绍二、在VisualStudio2019中创建.NetCoreWPF工程三、与.NetFramework的区别四、WPF中的XAML五、WPF中的布局六、WPF中的常用控件(上)七、WPF中的常用控件(中)八、WPF中的常用控件(下)其它、实现多语言切换的几种方......
  • bash: _get_comp_words_by_ref: command not found 报错
    没有安装补全的包错误信息bash:_get_comp_words_by_ref:commandnotfound表明你的shell中可能存在补全功能的问题。通常,这种错误发生在你的系统上未正确安装或配置bash-completion包时。这个包提供了kubectl和其他命令行工具所需的补全脚本。为了解决这个问题,你可以......
  • WPF implement ICommand and similar with DelegateCommand of Prism
    publicclassDelCmd:ICommand{publiceventEventHandlerCanExecuteChanged{add{CommandManager.RequerySuggested+=value;}remove{CommandManager.RequerySuggested-=......
  • WPF一个简单的属性编辑控件
    代码:publicclassPropertiesControl:Grid{[TypeConverter(typeof(LengthConverter))]publicdoubleRowHeight{get{return(double)GetValue(RowHeightProperty);}set{SetValue(RowHeightProperty,......
  • WPF 加载本地HTML
    index.html代码高亮:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>jQuery语法高亮示例</title><linkrel="stylesheet"href="https://cdnjs.cloudflare.com/a......
  • Rename – A Command Line Tool For Renaming Multiple Files in Linux
    from: https://www.tecmint.com/rename-multiple-files-in-linux/Weoftenusethe mvcommand torenameasinglefilein Linux.However,renamingmultipleorgroupsoffilesquicklymakesitaverydifficulttaskinaterminal.Linux comeswithaverypowerf......