原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理


  1. <ListBox ItemsSource="{Binding ClassCollection}">
  2. <ListBox.ItemContainerStyle>
  3. <Style TargetType="{x:Type ListBoxItem}">
  4. <Setter Property="Height" Value="30"/>
  5. <Setter Property="Template">
  6. <Setter.Value>
  7. <ControlTemplate TargetType="ListBoxItem">
  8. <Grid>
  9. <Border x:Name="ItemBackground"
  10. Background="{TemplateBinding Background}"
  11. BorderBrush="{TemplateBinding BorderBrush}"
  12. BorderThickness="{TemplateBinding BorderThickness}"/>
  13. <CheckBox Content="{Binding Item2.Name}" IsChecked="{Binding Item1,Mode=TwoWay}">
  14. <i:Interaction.Triggers>
  15. <i:EventTrigger EventName="Checked">
  16. <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Control_CodeAutoGeneration}}, Path=DataContext.RelayCommand}"
  17. CommandParameter="ClassSelectionChanged"/>
  18. </i:EventTrigger>
  19. <i:EventTrigger EventName="Unchecked">
  20. <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Control_CodeAutoGeneration}}, Path=DataContext.RelayCommand}"
  21. CommandParameter="ClassSelectionChanged"/>
  22. </i:EventTrigger>
  23. </i:Interaction.Triggers>
  24. </CheckBox>
  25. </Grid>
  26. </ControlTemplate>
  27. </Setter.Value>
  28. </Setter>
  29. </Style>
  30. </ListBox.ItemContainerStyle>
  31. <i:Interaction.Triggers>
  32. <i:EventTrigger EventName="SelectionChanged">
  33. <i:InvokeCommandAction Command="{Binding RelayCommand}" CommandParameter="ClassSelectionChanged"/>
  34. </i:EventTrigger>
  35. </i:Interaction.Triggers>
  36. </ListBox>

其中、需要在代码

<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Control_CodeAutoGeneration}}, Path=DataContext.RelayCommand}"   CommandParameter="ClassSelectionChanged"/>

处绑定的父级别,Control_CodeAutoGeneration是我自定义的控件的ViewModel里面的RelayCommand命令,通过如上传递可以调用到父级别的ViewModel的绑定属性或命令

总结:WPF中模板需要绑定父级别的ViewModel该如何处理的更多相关文章

  1. WPF中,如何将绑定源设置到单件实例

    原文:WPF中,如何将绑定源设置到单件实例  WPF中,如何将绑定源设置到单件实例                                       周银辉 大概两个月前,曾有位朋友问我:如 ...

  2. WPF中DatePiker值绑定以及精简查询

    WPF中DatePiker值绑定以及精简查询 1.WPF中DatePiker值绑定 Xaml中值绑定使用Text <DatePicker Text="{Binding strMinDa ...

  3. 关于WPF中ItemsControl系列控件中Item不能继承父级的DataContext的解决办法

    WPF中所有的集合类控件,子项都不能继承父级的DataContext,需要手动将绑定的数据源指向到父级控件才可以. <DataGridTemplateColumn Header="操作 ...

  4. javascript父级鼠标移入移出事件中的子集影响父级的处理方法

    一.我们先分析其产生的原因: 1.当鼠标从父级移入子集时触发了父级的两个事件:a.父级的mouseout事件(父级离开到子集):b.由于事件冒泡影响,又触发了父级的mouseover事件(父级移入父级 ...

  5. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

  6. wpf中的datagrid绑定操作按钮是否显示或者隐藏

    如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了. 但是在wpf里不行..网上 ...

  7. WPF中使用ObjectDataProvider绑定方法

    ObjectDataProvider提供了绑定任意.net类型的功能,具体功能如下: 1.ObjectDataProvider提供了绑定任意CLR类型的公嫩那个. 2.它可以再XAML中利用生命史的语 ...

  8. WPF中模板选择和DataContext的一些使用

    如图样: View结构 MainView(MainViewModel)|---Guide1View(Guide1ViewModel)|---Guide2View(Guide2ViewModel) |- ...

  9. wpf 中的DataTemplate 绑定控件

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

随机推荐

  1. Redis_数据类型

    Redis支持的键值数据类型如下: 字符串类型 散列类型 列表类型 集合类型 有序集合类型  一.字符串类型 字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据.一个字 ...

  2. 如何检查linux服务器是否被入侵

    被入侵服务器的症状 当服务器被没有经验攻击者或者自动攻击程序入侵了的话,他们往往会消耗 100% 的资源.他们可能消耗 CPU 资源来进行数字货币的采矿或者发送垃圾邮件,也可能消耗带宽来发动 DoS ...

  3. JLOI 2009 二叉树问题

    洛谷 P3884 [JLOI2009]二叉树问题 洛谷传送门 JDOJ 2024: [JLOI2009]二叉树问题 JDOJ传送门 Description 如下图所示的一棵二叉树的深度.宽度及结点间距 ...

  4. [RN] 阿里 ant-design 菜单比较丰富 https://github.com/ant-design/ant-design-mobile

    阿里 ant-design  菜单比较丰富 https://github.com/ant-design/ant-design-mobile 天和风雨顺 地和五谷丰 人和事业旺 家和万事兴

  5. [RN] React Native 权限申请 react-native-permissions

    React Native 权限申请 react-native-permissions 一.安装 npm i -S react-native-permissions link: react-native ...

  6. 如何保证redis数据都是热点数据

    mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据? 1.限定 Redis 占用的内存,Redis 会根据自身数据淘汰策略,加载热数据到内存.所以,计算 ...

  7. 安装-apache skywalking (java 应用性能监控)

    官网:http://skywalking.apache.org/ 服务器:10.30.31.28 centos 7 jdk 1.8.x ES 5.x 5.0.0-bet a2版本 . http://s ...

  8. 【IntelliJ IDEA学习之七】maven专题

    版本:IntelliJIDEA2018.1.4 一.maven全局设置 设置Runner 运行VM参数:-Xms128m -Xmx512m -Duser.language=zh -Dfile.enco ...

  9. python读取word中的段落、表、图+++++++++++Doc转换Docx

    读取文本.图.表.解压信息 import docx import zipfile import os import shutil '''读取word中的文本''' def gettxt(): file ...

  10. excel中怎么将行转换为列及列转换成行

    操作方法 01 选中要把行转换为列的数据.然后点击键盘上的ctrl+c. 02 在需要放置的单元格上,右键点击,然后点击菜单上的‘选择性粘贴’. 03 在弹出的窗口上,点击勾选上‘转置’.点击确定按钮 ...