仅重新设计 wpf 控件的一部分。 是否可以?

发布于 2024-07-25 15:13:33 字数 914 浏览 3 评论 0原文

我只想重新设计 WPF 控件的一部分。

(具体来说,我想让 ScrollBar 上的滚动按钮更大)

现在我可以毫无问题地做到这一点。 我提取了默认样式。 进行了所需的调整并将修改后的样式包含在我的应用程序中。

问题是,当我更改的只是 2 个值时,我必须在替换样式中包含整个控件模板。

  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="ScrollBar">
        <Grid Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
          <Grid.ColumnDefinitions>
            <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}" />
            <ColumnDefinition Width="1E-05*" />
            <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}" />
          </... etc

我只想将列定义上的 MaxWidth 更改为 50。

所以我的问题是:有什么方法可以对模板进行此调整,而不必包含其他所有内容? 我仍然希望控件模板的所有其他部分都像默认值一样工作。

I want to restyle just part of a WPF control.

(Specifically, I want to make the scroll buttons on a ScrollBar bigger)

Now I can do this no problem. I extracted the default style. Made the required tweaks and included the modified style in my application.

The problem is that I have to include the entire control template in the replacement style, when all I am changing is 2 values.

  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="ScrollBar">
        <Grid Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
          <Grid.ColumnDefinitions>
            <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}" />
            <ColumnDefinition Width="1E-05*" />
            <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}" />
          </... etc

I just want to change the MaxWidth on the column definitions to 50.

So my question is this: Is there any way I can make this tweak to the template without having to include everything else? I still want all other parts of the control template to just work like the default.

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

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

发布评论

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

评论(1

洋洋洒洒 2024-08-01 15:13:33

在这种情况下,您应该能够这样做,因为模板会伸出援手并从资源中获取值:

<ScrollBar xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ScrollBar.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}">50</sys:Double>
    </ScrollBar.Resources>
</ScrollBar>

如果模板对值进行了硬编码,那么您就不走运了。

In this case you should be able to, since the template reaches out and grabs the value from a resource:

<ScrollBar xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ScrollBar.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}">50</sys:Double>
    </ScrollBar.Resources>
</ScrollBar>

If the template had hard-coded the value, you'd be out of luck.

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