原文:总结: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. Mysql-MariaDB设置延迟同步

    CHANGE MASTER TO MASTER_DELAY = N; N为秒数

  2. Python学习日记(四十一) Mysql数据库篇 九

    前言 索引的主要作用是起到约束和加速查找,ORM框架(sqlalchemy)是用类和对象对数据库进行操作 索引的种类 按种类去分 1.普通索引:能够加速查找 2.主键索引:能够加速查找.不能为空.不能 ...

  3. istio部署-快速入门

    参考 istio/istio Quick Start Evaluation Install fleeto/sleep fleeto/flaskapp 本文为 istio 快速入门部署,一般用于演示环境 ...

  4. BSGS算法(大小步算法)

    $BSGS$ 算法 $Baby\ Steps\ Giant\ Steps$. 致力于解决给定两个互质的数 $a,\ p$ 求一个最小的非负整数 $x$ 使得 $a^x\equiv b(mod\ p)$ ...

  5. 第02节-BLE协议各层的形象化理解

    本篇博客根据韦大仙视频,整理所得. 先上框图: ATT层 从ATT开始看,在上篇博客讲的医院结构里面有个检验室,检验室可以得到各项结果,但是它并不知道这些结果代表什么含义.类比的在BLE协议栈里面,A ...

  6. acwing 算法面试、笔试题公开课整理记录

    week1 Google KickStart 2019 A轮 讲解视频地址AcWing 549. 训练   tag: 排序 遍历 在线练习地址AcWing 550. 包裹       在线练习地址Ac ...

  7. vue之非父子通信

    一.非父子通信: 思路: 找个中间存储器,组件一把信息放入其中,组件二去拿 代码如下: let hanfei = new Vue();  # 实列化个空的vue对象,作为中间存储器来时间        ...

  8. linux定时器的实现方法

    Linux提供定时器机制,可以指定在未来的某个时刻发生某个事件,定时器的结构如下: struct timer_list { struct list_head list; unsigned long e ...

  9. Codeforces Round 558(Div 2)题解

    这场比赛没有打,后来和同学们一起开了场镜像打…… B是SB题结果WA了5发…… C是SB题结果差5min调出……虽然中间有个老师讲题吃掉了1h D是比较神仙的题(2200),但是做出来了?算是比较超常 ...

  10. 将float数字按照一定格式写入到文件中

    /* float.c */ #include<stdio.h> #include<string.h> int main() { FILE *fp = fopen("D ...