user1145533
user1145533

Reputation: 697

DataTemplateTrigger not working with datatemplateselector

I have defined a datatemplate that has a trigger that should simply change the background color if the item is selected. For some reason it doesn't seem to be working.

        <DataTemplate x:Key="existingDeviceTemplate" >
            <StackPanel Orientation="Horizontal">
                <Border Name="bd" Background="Green"  BorderThickness="1" Padding="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding Path=Name}" Width="200"/>
                        <Button Grid.Column="1" Content="Settings" Click="cmdSettings_Clicked"/>
                        <Button Grid.Column="2" Content="Delete" Click="cmdDelete_Clicked"/>
                    </Grid>
                </Border>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="true" >
                    <Setter TargetName="bd" Property="Background" Value="Red"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

Upvotes: 0

Views: 248

Answers (2)

LPL
LPL

Reputation: 17063

Try to bind to the IsSelected property of the ListBoxItem:

<DataTemplate.Triggers>
     <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="true">
          <Setter TargetName="bd" Property="Background" Value="Red" />
     </DataTrigger>
</DataTemplate.Triggers>

If your list control is a ListView you have to replace x:Type ListBoxItem with x:Type ListViewItem.

Upvotes: 1

Tamir Dresher
Tamir Dresher

Reputation: 194

Does your binded model had a property named IsSelected? my guess is that you mean the rows' IsSelected property. If thats the case you need to put a RelativeSource binding with FindAncestor to the ListItem

Upvotes: 1

Related Questions