2012-01-30 15 views
10

मेरे पास निम्नलिखित XAML है। मैं आइटम नियंत्रण में चयनित आइटम को कैसे हाइलाइट कर सकता हूं? मैं ListView के लिए चयनित आइटम टेम्पलेट को ओवरराइड कर सकता हूं लेकिन आइटम्स कंट्रोल के लिए इसे कैसे प्राप्त किया जाए? क्या कोई वैकल्पिक नियंत्रण है जो छवियों का संग्रह प्रदर्शित कर सकता है?आइटम नियंत्रण में चयनित आइटम को हाइलाइट कैसे करें?

<Window x:Class="ImageScrollDemo.View.MoviePosterView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:ImageScrollDemo" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    Title="MoviePosterView" Height="367" Width="725" Foreground="White"> 
<Window.Background> 
    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 
     <GradientStop Color="#FF505050" Offset="0"/> 
     <GradientStop Color="#FF202020" Offset="1"/> 
    </LinearGradientBrush> 
</Window.Background> 
<Window.DataContext> 
    <local:MainWindowViewModel /> 
</Window.DataContext> 
<Window.Resources> 
    <local:MainWindowViewModel x:Key="ViewModel" /> 

    <Style TargetType="Image" x:Key="ImageHover"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Opacity" Value="1" /> 
       <Setter Property="BitmapEffect"> 
        <Setter.Value> 
         <OuterGlowBitmapEffect GlowColor="Gold" GlowSize="8"/> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="False"> 
       <Setter Property="Opacity" Value="0.7" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<i:Interaction.Triggers> 
    <i:EventTrigger EventName="Loaded"> 
     <i:InvokeCommandAction Command="{Binding PopulateMovieGrid}" /> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="60" /> 
    </Grid.RowDefinitions> 
    <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"> 
     <ItemsControl Name="itemsCtrl" HorizontalContentAlignment="Center" ItemsSource="{Binding Path=MovieCollection, Mode=TwoWay}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="5" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 

      <ItemsControl.ItemContainerStyle> 
       <Style TargetType="ContentPresenter"> 
        <Setter Property="ContentTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <Border BorderBrush="Black" BorderThickness="5"> 
            <ContentPresenter Content="{Binding}"/> 
           </Border> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ItemsControl.ItemContainerStyle> 
      <ItemsControl.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> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid> 

+0

एक ListBox उपयोग और SelectionMode तो सेट करना चाहिए = "एकाधिक" – 0x4f3759df

उत्तर

23

एक ItemsControl ट्रैक नहीं करता SelectedItem

आपको लगता है कि व्यवहार चाहते हैं, मैं एक 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

आप भी कर सकते हैं के लिए शैलियों और टेम्पलेट्स अधिलेखन सुझाव है अभिव्यक्ति मिश्रण अंतःक्रियाशीलता के माध्यम से। शीर्ष कंटेनर (StackPanel या ग्रिड) में आइटम टेम्पलेट में ईवेंट नाम MouseLeftDown या टैप किए गए ईवेंट (विंडोज 8.1 में) का उपयोग करें।

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> 

यह चाल

संबंधित मुद्दे