views:

17

answers:

3

I have the following ListView with a ListView.ItemTemplate:

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanelName="stackPanel" Orientation="Horizontal">
            <TextBoxName="textBoxOrg"
                Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingOrgText}"
                IsReadOnly="True"/>
            <TextBoxName="textBoxNew"
                Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingNewText}"
                AcceptsReturn="True"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

And the following ListViewItemStyle

<Style TargetType="ListViewItem">
    <Setter Property="SnapsToDevicePixels" Value="True" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightGoldenrodYellow" />
            </Trigger>
        </Style.Triggers>
</Style>

I want to change the default 'Blue' background color of the selected item, but when using the above code, it did not change to 'LightGoldenrodYellow' when I select an item.

How should I fix the code to let it work properly?

A: 

You need to customize the ControlTemplate of the ListViewItem. Otherwise it overrides(doesnt use) the Background trigger you defined. Here is the default template to customize (I'm using a helpful little tool called stylesnooper to get the templates http://wpfwonderland.wordpress.com/2007/01/02/wpf-tools-stylesnooper/):

            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True">
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                    </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="Selector.IsSelected">
                        <Setter Property="Panel.Background" TargetName="Bd">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        <Trigger.Value>
                            <s:Boolean>
                                True</s:Boolean>
                            </Trigger.Value>
                        </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelected">
                                <Condition.Value>
                                    <s:Boolean>
                                        True</s:Boolean>
                                    </Condition.Value>
                                </Condition>
                            <Condition Property="Selector.IsSelectionActive">
                                <Condition.Value>
                                    <s:Boolean>
                                        False</s:Boolean>
                                    </Condition.Value>
                                </Condition>
                            </MultiTrigger.Conditions>
                        <Setter Property="Panel.Background" TargetName="Bd">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                    <Trigger Property="UIElement.IsEnabled">
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        <Trigger.Value>
                            <s:Boolean>
                                False</s:Boolean>
                            </Trigger.Value>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
MrDosu
A: 

Dr WPF has a excellent article on ItemsControls called (ItemsControl: A to Z) and read I and L

rudigrobler
A: 

I finally work out like this:

<Style x:Key="myListboxStyle">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCFFFFFF" />
    </Style.Resources>
</Style>

And it works perfect. Thanks all of you.

Amemiya