簡體   English   中英

選中時保持ListBoxItem的常規樣式

[英]Keeping coustum style of ListBoxItem when selected

我有一個ListBox,其中項目的背景色綁定到條目的某些屬性:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
    <ListBox.ItemContainerStyle >
        <Style TargetType="ListBoxItem" >
            <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

這可行,但是當我將鼠標懸停或選擇一個項目時,背景會更改(默認為鼠標懸停/選定的顏色)(這並不奇怪)。

我是WPF的新手,我不確定自己是否會正確地執行此類操作,我想也許我需要使用ItemContainerStyleSelector,但是我對如何使用它感到困惑,這似乎很愚蠢。必須為這個小東西創建一個類...

我還想到的是創建一個從布爾到顏色的IValueConverter,然后通過綁定它而不必使用DataTrigger作為其他方法,這會更優雅嗎? 那會對我有什么幫助嗎?

編輯:

如果我可以不問太多的話,如果我可以將所選項目的背景顏色更改為基於AnotherPropertyOfFoo的另一種顏色,那也很好。

編輯2(擴展名@Sheridan的評論):

這不起作用

    <ListBox>
        <ListBox.Items>
            <ListBoxItem>one</ListBoxItem>
            <ListBoxItem>two</ListBoxItem>
            <ListBoxItem>three</ListBoxItem>
            <ListBoxItem>four</ListBoxItem>
        </ListBox.Items>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Background" Value="Green" />
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

您還可以覆蓋ListBoxItem的模板,使用blend提取默認值並覆蓋或使用此處已經提到的一些方法

編輯

實際上,重寫Template :)並不難,我想這是解決您的問題的最正確方法。 試試這種ItemContainer樣式。 它替換默認的ListBoxItem樣式->模板。 要查看其工作原理-在觸發器中,您可以更改列表框項目的任何屬性。

 <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Padding" Value="2,0,0,0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="Border" Property="Background" Value="LightBlue"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="Border" Property="Background" Value="Blue"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="Gray"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

您可以在此處找到ListBoxItem的默認樣式,以進行所需的修改。

嘗試使用此:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
    <ListBox.ItemContainerStyle >
        <Style TargetType="ListBoxItem" >
            <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

SystemColors.HighlightBrushKey代表集合控件中所選項目的默認背景顏色...在這里,它設置為Transparent ,但是您可以將其設置為您喜歡的顏色。

更新>>>

這段代碼很好用...如果將“ Resources部分中的第一個SolidColorBrush更改為Red ,則所選項目的背景色將為Red BindingAnotherPropertyOfFoo不會影響所選的項目,因為兩者之間沒有任何關系。 為此,您可以嘗試以下方法:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
    <ListBox.ItemContainerStyle >
        <Style TargetType="ListBoxItem" >
            <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{Binding AnotherPropertyOfFoo}" />
                </DataTrigger>
            </Style.Triggers>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

現在,所選項目將從AnotherPropertyOfFoo屬性獲取背景顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM