c# - 如何使用 WPF 和 XAML 更改 ItemSource 中最后一项的样式?
问题描述
我有一个在 Prism 6 之上使用 c# 和 WPF 编写的应用程序。
我正在尝试为用户显示面包屑,以便他们可以单击上一个链接返回上一页。我希望面包屑看起来像这样
首页>>祖父类>> 父类>>子类
所以“家”、“祖父类别”和“父类别”都是可点击的,后面跟着“>>”符号。但最后一项(即“子类别”)不可点击,并且后面没有“>>”符号。
这是我所做的XAML
<!-- Breadcrubs -->
<ItemsControl ItemsSource="{Binding BreadcrumbScreenCategories}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- This is the clickable style which is applied on all items -->
<StackPanel Orientation="Horizontal">
<Button Content="{Binding Title}"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}},
Path=DataContext.SelectBreadcrumbCategory}"
CommandParameter="{Binding}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Padding="0 7">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock Foreground="{DynamicResource PrimaryHueMidBrush}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Margin="{TemplateBinding Margin}" />
</TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text=" >> "
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我的代码的问题是每个类别都是可点击的,并且后面跟着“>>”符号。
问题,如何更改最后一项的样式?
这里是
解决方案
实现此目的的另一种方法是不更改样式,而是通过可见性转换器上的绑定来隐藏最后一项的 >> 元素。并且还禁用最后一个按钮 - 再次,通过绑定。因此,您的 VM 项目(ItemsSource 的项目)提供了一个类似的属性IsClickable
,它被设置true
为所有项目,但最后一个。
推荐阅读
- typescript - 同一类型的多个字段的接口
- python - 如何将随机键值对传递给 Python 中的函数/构造函数?
- mysql - 在配置文件 MySQL 8 中设置 LOCAL_INFILE
- swift - UIImage 未从 AWS S3 url 加载
- python - 如何使用 tksheet 将表格保存为 csv 格式的文件?
- javascript - ASP.NET 核心图像到 Canvas
- c# - 大点云中大点循环的优化
- android - 找不到兼容的并排 NDK 版本 React Native
- php - 无法在 Ubuntu 18.04 上安装 lsphp56
- c - c中的管道,2个叉子与main交谈