none
如何处理ListBox的Item被选中的trigger? RRS feed

  • 问题

  • 意图:当ListBox中的某个Item被选中(可能是用鼠标选择,也可能是代码选中),如何设置Trigger来更改其中TextBlock的颜色

    代码如下:

    <ListBox Name="listBox1" Height="58" ItemsSource="{Binding Persons}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Grid>
                        <TextBlock x:Name="Label" Text="{Binding Name}" FontSize="20"/>
                    </Grid>
                </Border>
    
                <DataTemplate.Triggers>
                    <EventTrigger RoutedEvent="当前item被选择???">
                        <Setter TargetName="Label"  Property="Foreground" Value="Red"/>
                    </EventTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    2017年8月11日 3:26

答案

  • Hi,

    尝试下面的代码,直接使用数据触发器,绑定ListBoxItem的IsSelected属性。

           <ListBox   ItemsSource="{Binding Persons}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Green" BorderThickness="1" >
                            <Grid >
                                <TextBlock x:Name="myTextBox" Text="{Binding Name}"  Foreground="Red" Background="Green" FontSize="20" />
                            </Grid>
                        </Border>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                                <Setter Property="Background" TargetName="myTextBox" Value="Blue"/>
                            </DataTrigger>   
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    你也可以尝试:通过属性触发器来设置不同的来Itemtemplate。

       <Window.Resources>
            <DataTemplate x:Key="ItemTemplate">
                <Border BorderBrush="Red" BorderThickness="1" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Grid>
                        <TextBlock x:Name="Label" Text="{Binding Name}" FontSize="20"/>
                    </Grid>
                </Border>
            </DataTemplate>
    
            <DataTemplate x:Key="SelectedTemplate">
                <Border BorderBrush="Green" BorderThickness="1">
                    <Grid>
                        <TextBlock Text="{Binding Name}" Foreground="Red" Background="Green" FontSize="20" />
                    </Grid>
                </Border>
           
            </DataTemplate>
    
            <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
                <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
    </Window.Resources>
     <ListBox Name="listBox1" Grid.Column="0"  ItemsSource="{Binding Persons}" ItemContainerStyle="{StaticResource ContainerStyle}" />

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月11日 5:25
    版主