无法在 WPF 中完全设置 ListBox/Scrollviewer 的样式

发布于 2024-08-16 04:01:04 字数 156 浏览 6 评论 0原文

我正在使用我们使用标准 ControlTemplates 创建的自定义滚动条,但是当我将它们应用到列表框时,右下角有一个角,我无法找到任何方法来覆盖。

不幸的是,在获得更多积分之前我无法发布图片。但我指的角落是当垂直和水平滚动条同时出现时,右下角有一个空间,填充了我无法覆盖的灰白色

I'm using custom Scrollbars we created using standard ControlTemplates, however when I apply them to a ListBox there is a corner in the bottom right which I am unable to find any way to override.

Unfortunately I can't post a picture until I get more points. But the corner I'm referring to is when both a vertical and horizontal scrollbar appear, there is a space in the bottom right that is filled with an off-white color that I am unable to ovrerride

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆蝶 2024-08-23 04:01:04

这是我使用 Blend 为 ScrollViewer 获得的模板代码的一部分。我在右下角添加了一个矩形并将填充设置为红色。您可以以相同的方式设置其样式,也可以使用 Grid.RowSpan="2" 表示 VerticalScrollBar(第一个)或 Grid.ColumnSpan="2" 表示 Horizo​​ntalScrollBar(第二个)来扩展其中一个 ScrollBar 以覆盖空间。

<Style TargetType="{x:Type ScrollViewer}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <ScrollContentPresenter Grid.Column="0"/>
                    <ScrollBar Name="PART_VerticalScrollBar" Grid.Row="0" Grid.Column="1" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                    <ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Value="{TemplateBinding HorizontalOffset}" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                    <Rectangle Grid.Row="1" Grid.Column="1" Fill="Red"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

this is the part of the template code i got for ScrollViewer using Blend. I added a Rectangle in the bottom right corner and set the Fill to Red. You can style it in the same way or you can expand one of the ScrollBar to cover the space using Grid.RowSpan="2" for VerticalScrollBar(first one) or Grid.ColumnSpan="2" for HorizontalScrollBar(second one).

<Style TargetType="{x:Type ScrollViewer}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <ScrollContentPresenter Grid.Column="0"/>
                    <ScrollBar Name="PART_VerticalScrollBar" Grid.Row="0" Grid.Column="1" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                    <ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Value="{TemplateBinding HorizontalOffset}" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                    <Rectangle Grid.Row="1" Grid.Column="1" Fill="Red"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
£冰雨忧蓝° 2024-08-23 04:01:04

另外两个解决方案有效。

1) 矩形颜色是动态的,通过键“SystemColors.ControlBrushKey”。您可以以自定义样式或 ScrollViewer 控件(或其祖先之一)的资源覆盖此键。来源:MSDN 上的这个问题

示例:

<!-- In a style -->
<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </Style.Resources>
</Style>

<!-- Or in the control -->
<ScrollViewer>
    <ScrollViewer.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </ScrollViewer.Resources>
</ScrollViewer>

2) 设置(与上面相同的位置)具有“隐藏”可见性的矩形样式。 警告:如果矩形包含在控件的后代中,这将会产生副作用。

<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
    <Style.Resources>
        <!-- 'BasedOn' can be omitted -->
        <Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
            <Setter Property="Visibility" Value="Hidden"/>
        </Style>
    </Style.Resources>
</Style>

Another two solutions work.

1) The rectangle colour's is dynamic, with the key "SystemColors.ControlBrushKey". You can override this key in a custom style, or the resources of the ScrollViewer control (or one of its ancestors). Source: this question on MSDN.

Example :

<!-- In a style -->
<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </Style.Resources>
</Style>

<!-- Or in the control -->
<ScrollViewer>
    <ScrollViewer.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </ScrollViewer.Resources>
</ScrollViewer>

2) Set (same places as above) the style of Rectangle with a "Hidden" visibility. Warning: this will have side-effetcs if rectangles are contained within the control's descendants.

<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
    <Style.Resources>
        <!-- 'BasedOn' can be omitted -->
        <Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
            <Setter Property="Visibility" Value="Hidden"/>
        </Style>
    </Style.Resources>
</Style>
赴月观长安 2024-08-23 04:01:04

有两件事可能会有所帮助:

1)使用 Snoop 探索应用程序的元素树,这可能会有所帮助找到问题所在。

2) 根据您启动控件的方式,您可能会考虑从标准列表框的副本开始。当我从空模板或部分模板开始设计样式时,我发现某些控件存在问题。

希望有帮助

Two things that might help:

1) Use Snoop to explore the element tree of your application, this might help in finding the problem.

2) Depending on how you started your control, you might consider starting from a copy of the standard ListBox. I've found problems with certain controls when I start styling from an empty or partial template.

hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文