【问题标题】:Add custom combobox scrollbar style wpf添加自定义组合框滚动条样式 wpf
【发布时间】:2020-02-15 12:58:57
【问题描述】:

我知道我可以在 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>

【问题讨论】:

    标签: wpf combobox scrollbar styling


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2016-09-02
      • 1970-01-01
      相关资源
      最近更新 更多