Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF change visual state of datatemplate when selected in a listbox

If I have a WPF ListBox which has a basic ItemTemplate that contains a custom user control, how can I tell the user control in the DataTemplate to change its visual state when its selected in the ListBox?

Thanks a lot for any help you can give

like image 727
Mark Avatar asked Jan 07 '11 04:01

Mark


1 Answers

You can style the ListBoxItem to trigger on the IsSelected property. Here's an example:

  • ListBoxItem ControlTemplate Example

and then you use it like this:

<ListBox ItemContainerStyle="{StaticResource yourListBoxItemStyle}">

or directly in the ListBox itself:

<ListBox.ItemContainerStyle>
    <Style TargetType=”ListBoxItem”>
        ...
    </Style>
</ListBox.ItemContainerStyle>

Edit:

Here is a complete example with both an ItemTemplate and an ItemContainerStyle that puts a semi-opaque layer on top of the selected item.

<Grid>
    <Grid.Resources>
        <x:Array Type="sys:String" x:Key="sampleData">
            <sys:String>Red</sys:String>
            <sys:String>Green</sys:String>
            <sys:String>Blue</sys:String>
        </x:Array>
        <DataTemplate x:Key="listBoxItem">
            <Rectangle Fill="{Binding}" Width="100" Height="100"/>
        </DataTemplate>
        <Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid>
                            <ContentPresenter />
                            <Rectangle x:Name="Rectangle" Fill="Black" Opacity="0"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Rectangle" Property="Opacity"
                                    Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <ListBox
        ItemsSource="{StaticResource sampleData}"
        ItemTemplate="{StaticResource listBoxItem}"
        ItemContainerStyle="{StaticResource listBoxItemStyle}"
        />
</Grid>

Edit

After a comment, here is the version using a "unified" template:

<Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid>
                    <Rectangle Name="Rectangle" Fill="{Binding}" Width="100" Height="100" Opacity="1"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Rectangle" Property="Opacity"
                            Value="0.5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 157
Rick Sladkey Avatar answered Nov 08 '22 01:11

Rick Sladkey