造型ListBoxItem中的嵌套列表框的[WPF] [英] Styling ListBoxItem's of nested ListBoxes [WPF]

查看:672
本文介绍了造型ListBoxItem中的嵌套列表框的[WPF]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框 ListBoxItem的 s表示包含嵌套列表框。现在我想在顶级列表框的项目没有蓝色背景,然后他们选择(参见图)。

I have a ListBox with ListBoxItems that contains a nested ListBox. Now I want the top-level ListBox's items to not have a blue background then they are selected (see pic).

我试图用XAML下面的样式来改变背景颜色为透明的descibed在此<一个href="http://blogs.msdn.com/wpfsdk/archive/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox.aspx"相对=nofollow>博客文章。这工作,但它也改变了项目的选择背景色嵌套列表框。如何更改它仅适用于顶级项目?

I tried to use the XAML below with a style to change the background color to transparent as descibed in this blog post. This works, but it also changes the selected background color of the items in the nested ListBox. How do I change it to only apply to the top-level items?

<Window x:Class="WpfTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="350" Width="352">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="SampleItemTemplate">
                <Border Margin="5" Background="Gray" CornerRadius="3">
                    <Border.BitmapEffect>
                        <DropShadowBitmapEffect/>
                    </Border.BitmapEffect>
                    <StackPanel Margin="2">
                        <TextBlock Text="{Binding Path=Lid, StringFormat='ProvID: {0}'}"/>
                        <ListBox ItemsSource="{Binding Path=TestOrders}"/>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </Grid.Resources>
        <Button Height="23" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Margin="0,0,12,12" Click="button1_Click">Button</Button>
        <ListBox Name="listBox1" Margin="12,12,12,41" ItemsSource="{Binding}" ItemTemplate="{StaticResource SampleItemTemplate}">
            <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
            </ListBox.Resources>
        </ListBox>
    </Grid>
</Window>

据我所知,这种风格适用于类型的所有对象 ListBoxItem的,但我怎么得到它仅适用于在ListBox1的项目?

I understand that this style applies to all objects of type ListBoxItem, but how do I get it to apply only to items in listbox1?

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </Style.Resources>
</Style>

也许有更好的方式来完成我想要的,在这种情况下,请告诉我!

Probably there is a better way to accomplish what I want, in that case, please tell me!

推荐答案

你既需要内与外的项目是可选择的?我可能会用的ItemsControl坚持的外部列表,只是让内心的项目是可选择的。然后,你可以风格ListBoxItem的,而不会影响外的项目。

Do you need both the "inner" and "outer" items to be selectable? I would probably stick with ItemsControl for the outer list and just let the inner items be selectable. Then you could style ListBoxItem without affecting the "outer" items.

这篇关于造型ListBoxItem中的嵌套列表框的[WPF]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆