How to highlight selected item in ItemsControl?

后端 未结 2 1259
无人共我
无人共我 2020-12-10 14:17

I have the following XAML. How can i highlight the selected item in the ItemsControl ? I can override the selected item template for ListView but how to achieve the same for

相关标签:
2条回答
  • 2020-12-10 15:07

    you can also do this through the expression blend interactivity. Use the event name MouseLeftDown or Tapped Event (in Windows 8.1) in the ItemsTemplate in the top container (StackPanel or the grid).

    xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftDown">
            <Core:ChangePropertyAction PropertyName="Background" Value="Green"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    

    This should do the trick

    0 讨论(0)
  • 2020-12-10 15:09

    An ItemsControl does not track the SelectedItem

    If you want that behavior, I would suggest overwriting the styles and templates for a ListBox

    <ListBox Name="itemsCtrl" HorizontalContentAlignment="Center" ItemsSource="{Binding Path=MovieCollection, Mode=TwoWay}">
    
        <ListBox.Resources>
            <!-- Set SelectedItem Background here -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>
        </ListBox.Resources>
    
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="5" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    
        <ListBox.ItemTemplate>
            <DataTemplate>
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                        <Image Style="{StaticResource ImageHover}" Margin="0,5,0,0" RenderOptions.BitmapScalingMode="HighQuality" SnapsToDevicePixels="True" HorizontalAlignment="Center" Source="{Binding Path=Poster}" Height="150" />
                        <TextBlock Name="txtTitle" FontSize="10" HorizontalAlignment="Center" TextAlignment="Justify" Text="{Binding Title}" TextWrapping="Wrap" MaxWidth="110" />
                    </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
    0 讨论(0)
提交回复
热议问题