添加自定义组合框滚动条样式WPF

时间:2020-02-15 12:58:57

标签: wpf combobox scrollbar styling

我知道我可以在http://mark-dot-net.blogspot.com/2008/06/styling-listbox-with-silverlight-2-beta_21.html上找到它,但是我希望能够做这样的事情:

<ComboBox Width="163" Height="30">
     <ComboBox.Style>
          <Style>
              <Setter Property="ScrollViewer" Value="{StaticResource Custom}"/>
          </Style>
     </ComboBox.Style>
</ComboBox>

但是由于ScrollViewer不可用,我尝试执行以下操作:

<ComboBox Width="163" Height="30">
     <Style>
         <Setter Property="ScrollViewer.Style" Value="{StaticResource Custom}"/>
     </Style>
</ComboBox>

但是它不起作用,如果我停留在Combobox.Style中,它会给我一个错误Style object is not allowed to affect the Style property of the object to which it applies to.,我也尝试做过ScrollViewer.Template,但是当我执行此操作时,我的计算机崩溃了:有没有一种方法,而无需使用我之前提供的链接中的方法?我的风格xaml是:

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition MaxHeight="0"/>
                <RowDefinition Height="0.00001*"/>
                <RowDefinition MaxHeight="0"/>
            </Grid.RowDefinitions>
            <Border Grid.RowSpan="3" CornerRadius="2" Background="Transparent" />
            <RepeatButton Grid.Row="0" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineUpCommand" Content="M 0 4 L 8 4 L 4 0 Z" />
            <Track Name="PART_Track" Grid.Row="1" IsDirectionReversed="true">
                <Track.DecreaseRepeatButton>
                    <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageUpCommand" />
                </Track.DecreaseRepeatButton>
                <Track.Thumb>
                    <Thumb Style="{StaticResource ScrollBarThumb}" Margin="1,0,1,0" Background="#E43D47" BorderBrush="{StaticResource HorizontalNormalBorderBrush}" />
                </Track.Thumb>
                <Track.IncreaseRepeatButton>
                    <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageDownCommand" />
                </Track.IncreaseRepeatButton>
            </Track>
            <RepeatButton Grid.Row="3" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineDownCommand" Content="M 0 0 L 4 4 L 8 0 Z"/>
        </Grid>
    </ControlTemplate>

1 个答案:

答案 0 :(得分:1)

只需在ComboBox的Resources块中声明ScrollViewer样式:

<ComboBox ItemsSource="{Binding Items}">
    <ComboBox.Resources>
        <Style TargetType="ScrollViewer">
            <Setter Property="Background" Value="Green" />
            <Setter Property="Padding" Value="20" />
        </Style>
    </ComboBox.Resources>
</ComboBox>
相关问题