Witcher
Witcher

Reputation: 1140

ItemContainerStyle blocks ItemContainerStyleSelector

I have the code:

<ListBox Style="{StaticResource DeviceListBox}"
                 ItemsSource="{Binding MeterList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                 SelectedItem="{Binding CurrentMeter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                 ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Style="{StaticResource DeviceListText}" Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

I use ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}" to change background color in each listbox items (e.g. black or silver, see - http://msdn.microsoft.com/en-us/library/system.windows.controls.styleselector.aspx). And it works. But if I add ItemContainerStyle="{StaticResource DeviceListItemStyle}" to create some triggers etc in DeviceListItemStyle then DeviceListItemStyleSelector doesn't work. Help me, please!)

Upvotes: 0

Views: 4851

Answers (1)

Rachel
Rachel

Reputation: 132618

The ItemContainerStyleSelector selects a Style based on some logic, so obviously setting the Style manually will overwrite whatever Style your Selector applied.

Why don't you just set the background color in your ItemContainerStyle?

<Style x:Key="DeviceListItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSilver}" Value="True">
            <Setter Property="Background" Value="Silver" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 6

Related Questions