Oleg Sh
Oleg Sh

Reputation: 9013

Change property on ListBox.SelectionChanged in XAML

I want to write XAML code to change color of button when user changes selectionIndex of ListBox. How can I do it? I try

<Trigger Property="IsSelectionChanged" Value="True">
    <Setter TargetName="btnSave" Property="Background" Value="Red" />
</Trigger>

But property IsSelectionChanged is not found.

Upvotes: 0

Views: 2613

Answers (1)

brunnerh
brunnerh

Reputation: 184296

There is no such property, you need to use an EventTrigger:

<Button Name="buton" Content="The Buton"/>
<ListBox ItemsSource="{Binding Data}">
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="SelectionChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference buton}"
                                    Storyboard.TargetProperty="Background">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Brushes.Red}" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

Upvotes: 3

Related Questions