ControlTemplate in WPF —— ListBox

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Shared.xaml" />
  </ResourceDictionary.MergedDictionaries>

  <!--<SnippetListBox>-->
  <Style x:Key="{x:Type ListBox}"
         TargetType="ListBox">
    <Setter Property="SnapsToDevicePixels"
            Value="true" />
    <Setter Property="OverridesDefaultStyle"
            Value="true" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll"
            Value="true" />
    <Setter Property="MinWidth"
            Value="120" />
    <Setter Property="MinHeight"
            Value="95" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ListBox">
          <Border Name="Border"
                  BorderThickness="1"
                  CornerRadius="2">
            <Border.Background>
              <SolidColorBrush Color="{StaticResource ControlLightColor}" />
            </Border.Background>
            <Border.BorderBrush>
              <SolidColorBrush Color="{StaticResource BorderMediumColor}" />
            </Border.BorderBrush>
            <ScrollViewer Margin="0"
                          Focusable="false">
              <StackPanel Margin="2"
                          IsItemsHost="True" />
            </ScrollViewer>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled"
                     Value="false">
              <Setter TargetName="Border"
                      Property="Background">
                <Setter.Value>
                  <SolidColorBrush Color="{StaticResource DisabledControlLightColor}" />
                </Setter.Value>
              </Setter>
              <Setter TargetName="Border"
                      Property="BorderBrush">
                <Setter.Value>
                  <SolidColorBrush Color="{DynamicResource DisabledBorderLightColor}" />
                </Setter.Value>

              </Setter>
            </Trigger>
            <Trigger Property="IsGrouping"
                     Value="true">
              <Setter Property="ScrollViewer.CanContentScroll"
                      Value="false" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Type ListBoxItem}"
         TargetType="ListBoxItem">
    <Setter Property="SnapsToDevicePixels"
            Value="true" />
    <Setter Property="OverridesDefaultStyle"
            Value="true" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
          <Border x:Name="Border"
                  Padding="2"
                  SnapsToDevicePixels="true">
            <Border.Background>
              <SolidColorBrush Color="Transparent" />
            </Border.Background>
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="SelectionStates">
                <VisualState x:Name="Unselected" />
                <VisualState x:Name="Selected">
                  <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                  Storyboard.TargetProperty="(Panel.Background).
                      (SolidColorBrush.Color)">
                      <EasingColorKeyFrame KeyTime="0"
                                           Value="{StaticResource SelectedBackgroundColor}" />
                    </ColorAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="SelectedUnfocused">
                  <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                  Storyboard.TargetProperty="(Panel.Background).
                      (SolidColorBrush.Color)">
                      <EasingColorKeyFrame KeyTime="0"
                                           Value="{StaticResource SelectedUnfocusedColor}" />
                    </ColorAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ContentPresenter />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <!--</SnippetListBox>-->
</ResourceDictionary>

 

转载于:https://www.cnblogs.com/tianciliangen/p/7410549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中的ListBox控件可以自定义滚动条样式。ListBox的滚动条由两个部分组成:Track和Thumb。 要自定义ListBox的滚动条样式,我们首先需要创建一个新的Style,并在其中定义ScrollViewer的模板。ScrollViewer用于承载ListBox的内容并提供滚动功能。 以下是一个示例的ListBox滚动条样式: ```xaml <ListBox> <ListBox.Resources> <Style TargetType="ScrollViewer"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ScrollViewer"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Row="1" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/> <ScrollContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" ContentTemplate="{TemplateBinding ContentTemplate}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> </ListBox> ``` 在上述代码中,我们定义了一个新的Style并将其应用于ListBox的ScrollViewer。在这个Style中,我们定义了一个Grid,其中包含了一个ScrollBar和一个ScrollContentPresenter。ScrollBar用于显示和控制垂直滚动条,而ScrollContentPresenter用于显示ListBox的内容。 通过修改ScrollBar的样式,我们可以自定义滚动条的外观,例如修改Thumb的背景色、修改滚动条的宽度等等。 通过自定义ListBox的滚动条样式,我们可以实现更加个性化的界面效果,增强用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值