首页 > 解决方案 > 如何使用 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>

我的代码的问题是每个类别都是可点击的,并且后面跟着“>>”符号。

问题,如何更改最后一项的样式?

这里是

标签: c#wpfxamlwpf-controls

解决方案


实现此目的的另一种方法是不更改样式,而是通过可见性转换器上的绑定来隐藏最后一项的 >> 元素。并且还禁用最后一个按钮 - 再次,通过绑定。因此,您的 VM 项目(ItemsSource 的项目)提供了一个类似的属性IsClickable,它被设置true为所有项目,但最后一个。


推荐阅读