开发者

Change property on ListBox.SelectionChanged in XAML

开发者 https://www.devze.com 2023-03-09 02:55 出处:网络
I want to write XAML code to change color of button when user changes selectionIndex of ListBox. How can I do it? I try

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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号