【问题标题】:change the style of the selecteditem in list box更改列表框中选定项的样式
【发布时间】:2012-03-19 12:41:23
【问题描述】:

我有一个 ScrollViewer:

<ScrollViewer Width="160" 
              VerticalScrollBarVisibility="Auto" 
              HorizontalScrollBarVisibility="Hidden" 
              Height="324" Canvas.Top="0" 
              BorderThickness="0" Padding="0">
   <ListBox x:Name="Snapshots" SelectionChanged="Snapshots_SelectionChanged" 
            Padding="0" Background="#FFF0F0F0" 
            BorderBrush="{x:Null}" VerticalAlignment="Top" 
            SelectionMode="Single">
      <ItemsControl.ItemTemplate>
         <DataTemplate>
            <Image Source="{Binding imageSource}" 
                   Margin="5" Stretch="UniformToFill" 
                   Width="120" Opacity="0.2"/>
         </DataTemplate>
      </ItemsControl.ItemTemplate>
      <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"  
                        VerticalAlignment="Top"  HorizontalAlignment="Center"/>
         </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
   </ListBox>
</ScrollViewer>

如何实现这些功能:

  1. 更改所选项目(图像)的不透明度。
  2. 更改所选项目(图片)的默认边框样式。

【问题讨论】:

    标签: xaml silverlight listbox visualstatemanager selecteditemtemplate


    【解决方案1】:

    在您的ListBox 中更改ItemContainerStyle。来自MSDN的小样本:

    <Style x:Key="ItemContainerStyle" TargetType="ListBoxItem">
        ...
       <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="ListBoxItem">
                <Grid Background="{TemplateBinding Background}">
                   <vsm:VisualStateManager.VisualStateGroups>
                      ...
                      <vsm:VisualStateGroup x:Name="SelectionStates">
                         <vsm:VisualState x:Name="Unselected" />
                         <vsm:VisualState x:Name="Selected">
                            <Storyboard>
                               <DoubleAnimation 
                                         Storyboard.TargetName="contentPresenter" 
                                         Storyboard.TargetProperty="Opacity" 
                                         Duration="0" To=".75"/>
                            </Storyboard>
                         </vsm:VisualState>
                      </vsm:VisualStateGroup>
                         ...
                   </vsm:VisualStateManager.VisualStateGroups>
                   <ContentPresenter
                           x:Name="contentPresenter"
                           Content="{TemplateBinding Content}"
                           ContentTemplate="{TemplateBinding ContentTemplate}"
                           HorizontalAlignment="{TemplateBinding
                                                      HorizontalContentAlignment}"
                           Margin="{TemplateBinding Padding}"/>
                 </Grid>
              </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    
    <ListBox ItemContainerStyle="{StaticResource ItemContainerStyle}" 
             x:Name="Snapshots" 
             SelectionChanged="Snapshots_SelectionChanged" Padding="0"
             Background="#FFF0F0F0" 
             BorderBrush="{x:Null}" 
             VerticalAlignment="Top" SelectionMode="Single">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding imageSource}" Margin="5" 
                       Stretch="UniformToFill" Width="120" Opacity="0.2"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"  
                            VerticalAlignment="Top"  
                            HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ListBox>
    

    您还可以在Selected VisualState 中使用边框制作动画。

    【讨论】:

      【解决方案2】:

      谢谢你, 它现在正在工作。 这是我更新后的风格:

      <Style x:Key="ItemContainerStyle" TargetType="ListBoxItem">
              <Setter Property="Template">
                  <Setter.Value>
                     <ControlTemplate TargetType="ListBoxItem">
                          <Grid Background="{TemplateBinding Background}">
      
                              <vsm:VisualStateManager.VisualStateGroups>
                                  <vsm:VisualStateGroup x:Name="SelectionStates">
                                      <vsm:VisualState x:Name="Unselected" />
      
                                      <vsm:VisualState x:Name="Selected">
                                          <Storyboard>
                                              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageBorder" Storyboard.TargetProperty="Visibility" Duration="0">
                                                  <DiscreteObjectKeyFrame KeyTime="0">
                                                      <DiscreteObjectKeyFrame.Value>
                                                          <Visibility>Visible</Visibility>
                                                      </DiscreteObjectKeyFrame.Value>
                                                  </DiscreteObjectKeyFrame>
                                              </ObjectAnimationUsingKeyFrames>
                                              <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                          </Storyboard>
      
                                      </vsm:VisualState>
                                  </vsm:VisualStateGroup>
                              </vsm:VisualStateManager.VisualStateGroups>
                              <ContentPresenter
                                    x:Name="contentPresenter"
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Opacity="0.2"
                                    Margin="{TemplateBinding Padding}" />
                              <Border BorderBrush="Black" BorderThickness="5" x:Name="ImageBorder" Visibility="Collapsed"/>
                          </Grid>
      
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      这是列表框:

       <ScrollViewer Width="160" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" Height="324" Canvas.Top="0" BorderThickness="0" Padding="0">
                                  <ListBox ItemContainerStyle="{StaticResource ItemContainerStyle}" x:Name="ListBox_AcceptedPhotos" SelectionChanged="Snapshots_SelectionChanged" Padding="0" Background="#FFF0F0F0" BorderBrush="{x:Null}" VerticalAlignment="Top" SelectionMode="Single">
                                      <ItemsControl.ItemTemplate>
                                          <DataTemplate>
                                                  <Image Source="{Binding imageSource}" Margin="5" Stretch="UniformToFill" Width="120" MouseLeftButtonUp="Image_MouseLeftButtonUp" />
                                          </DataTemplate>
                                      </ItemsControl.ItemTemplate>
                                      <ItemsControl.ItemsPanel>
                                          <ItemsPanelTemplate>
                                              <StackPanel Orientation="Vertical"  VerticalAlignment="Top"  HorizontalAlignment="Center"/>
                                          </ItemsPanelTemplate>
                                      </ItemsControl.ItemsPanel>
                                  </ListBox>
                              </ScrollViewer>
      

      【讨论】:

      • xmlns:vsm = "clr-命名空间:System.Windows;assembly=System.Windows"
      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2017-04-18
      相关资源
      最近更新 更多