WPF Trigger for IsSelected in a DataTemplate for ListBox items
<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}">
<vw:HeaderSlugView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ContentSlugViewModel}">
<vw:ContentSlugView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ImageSlugViewModel}">
<vw:ImageSlugView />
</DataTemplate>
Each "View" is an independent XAML file. I'd like to be able to set up the triggers in those files, looking at the ListBoxItem's IsSelected property, in order to control the visibility of the various controls within.
The template to override the ListBoxItem follows:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="SlugContainer" Background="Transparent" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="0,5,0,0" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- snipped for brevity -->
<ContentPresenter Grid.Row="1" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I modified the ContentPresenter in the following way in order to test out using the "FindAncestor" RelativeSource:
<ContentPresenter Grid.Row="1">
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
This works, but when I move similar code into the XAML file representing a View it no longer sees the trigger. For example:
<UserControl ...>
<UserControl.Resources>
<local:FlowDocumentToXamlConverter x:Key="flowDocumentConverter" />
</UserControl.Resources>
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<DockPanel Name="SlugContainer">
<Label DockPanel.Dock="Top" Content="Filler" />
<ctrl:BindableRichTextBox x:Name="TextBox" Document="{Binding Content, Converter={StaticResource flowDocumentConverter}, Mode=TwoWay}" LostFocus="OnLostFocus" />
</DockPanel>
</UserControl>
How can I detect, preferably from the View's XAML file, when the ListBoxItem has been selected?
I notice that DataTrigger.Value property is missing in UserContrl's Style. With the following changes, I think your code would work.
<Style TargetType="{x:Type UserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
WPF Trigger for IsSelected in a DataTemplate for ListBox items的更多相关文章
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板
有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...
- [WPF系列]-数据邦定之DataTemplate 对 ItemsControl 进行样式和模板处理
引言 即使 ItemsControl 不是 DataTemplate 所用于的唯一控件类型,将 ItemsControl 绑定到集合仍然很常见. 在 DataTemplate 中有哪些内容一节中, ...
- [WPF系列]-数据邦定之DataTemplate 对分层数据的支持
到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...
- [WPF系列]-数据邦定之DataTemplate 使用 DataTrigger 来应用属性值
使用 DataTrigger 来应用属性值 当前表示不会告诉我们某个 Task 是家庭任务还是办公室任务.记住 Task 对象拥有类型为 TaskType 的 TaskType 属性,该类型是一个枚举 ...
- [WPF系列]-数据邦定之DataTemplate简介
引言 WPF 数据模板化模型为定义数据的表示形式提供了很大的灵活性.WPF 控件有支持自定义数据表示形式的内置功能.首先介绍下如何定义Datatemplate,然后再介绍其他数据模板化功能,例如根据自 ...
- WPF(WP7、WP8)多个Listbox嵌套时滚动问题的解决
内部的ListBox加属性 ScrollViewer.VerticalScrollBarVisibility="Disabled" 即可 如果不需要滚动,可以考虑嵌套换成 Item ...
- WPF 中使用MVVM模式后,找回ListBox中的ListBoxItem元素
ListBoxItem lstitem = this.list.ItemContainerGenerator.ContainerFromItem(m) as ListBoxItem; 其中this.l ...
- WPF触发器(Trigger)
WPF触发器(Trigger.DataTrigger.EventTrigger) WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改 ...
随机推荐
- JavaScript正则表达式匹配中英文以及常用标点符号白名单写法
我们在编程中经常会遇到特殊字符过滤的问题,今天我们提供一种白名单方式过滤 直接上代码 function RegEXP(s) { var rs = ""; for (var i = ...
- ASP.net MVC4/MVC5 错误处理及设置
很无聊的记录下.首先IIS 7以上的版本测试才成功 首先 //全局ErrorHandler public static void RegisterGlobalFilters(GlobalFilterC ...
- DDoS防护之TCP防护
本文由 网易云 发布. TCP协议,相信对于每一个开发工程师都不陌生.由于该协议是一个面向连接,可靠的特性,广泛应用于现在互联网的应用中.如常见的Web.SSH.FTP等都是基于TCP协议.目前TC ...
- 用 python 修改文件中指定的行数
#! /bin/python filename='setup.ini' lines=[] with open(filename,'r') as f: lines=f.readlines() lines ...
- JQuery Mobile - 修改复选框的选中状态无效解决办法!
今晚,在编写JQuery Mobile程序时候,需要在代码里面控制复选框的选中状态,很简单的代码啊,很快完成了!等测试程序时候傻眼了,页面无论如何也不按照我写的代码显示出来!问题出在哪里呢?是我写的控 ...
- Mac OS 10.12 - ”ln: /usr/bin/tclsh: Operation not permitted“错误的解决方法!!
我在对"/usr/bin/"进行创建链接时候,出现错误:”ln: /usr/bin/tclsh: Operation not permitted“,这个错误的原因是Rootless ...
- javascript中的数据类型和变量
Number JavaScript不区分整数和浮点数,统一用Number表示,以下都是合法的Number类型: 123; // 整数123 0.456; // 浮点数0.456 1.2345e3; / ...
- Java并发编程之happens-before
happens-before是JMM最核心的概念,理解happens-before是理解JMM的关键. 一.JMM的设计 首先,让我们先分析一下JMM的设计意图.从JMM的设计者的角度,在设计JMM的 ...
- jmeter制造大批量的用户数据数据
需求:因测试需要,要造100w用户数据,通过用户名.手机号.密码可新增用户,其中用户名和电话号码要求100w用户不能重复 要点: 1.通过Bean shell Sampler实现用户名和手机号的足够随 ...
- 【learning】微信跳一跳辅助c++详解 轻松上万 【下】
如果你还没有看完本blog的上篇,建议您先看完上篇!! 第一代辅助如何死的? 我们先来看四张图 如上方最左图所示,前面是一个小圆柱子,看起来很人畜无害似不似?? 由于上一步跳出了偏差,并没有 ...