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

Add custom combobox scrollbar style wpf

我知道我可以在 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 但是当我这样做时我的电脑崩溃了 / : 有没有办法不使用我之前提供的 link 中的方法来做到这一点?我的风格 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>

只需在 ComboBox 的资源块中声明一个 ScrollViewer 样式即可:

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