控件模板上的 WPF 设置

发布于 2024-08-23 06:30:11 字数 160 浏览 6 评论 0原文

一般问题。我有一个相当复杂的 ControlTemplate。几个文本框等 我无法使用 TemplateBinding 将所有属性带到表面,以便我可以设置所有样式。

有没有办法让样式“深入”控件内的控件来设置值?

希望我的问题在没有例子的情况下很清楚。

谢谢

General question. I have a ControlTemplate that is reasonably complex. Several TextBoxes etc.
I can't use TemplateBinding to bring all the properties to the surface so that I can set all the styles.

Is there a way for a Style to 'delv' into the controls within a control to set values?

Hope my question is clear without an example.

Thanks

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

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

发布评论

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

评论(1

傲鸠 2024-08-30 06:30:11

简短的回答是否定的。 ControlTemplate 本质上是一个黑匣子,至少在 XAML 方面是如此(有一些方法可以在代码中挖掘可视化树)。

当你说“不能使用TemplateBinding”时,为什么不呢?如果您没有足够的可用属性,可以通过为要传递的值创建一些附加属性来修复。这是假设您正在模板化一个无法更改的控件,否则您只需添加新的依赖项属性即可。

附加属性:

public static class CustomProps
{
    public static readonly DependencyProperty MyNewBrushProperty = DependencyProperty.RegisterAttached(
        "MyNewBrush",
        typeof(Brush),
        typeof(CustomProps),
        new UIPropertyMetadata(Brushes.Green));

    public static Brush GetMyNewBrush(DependencyObject target)
    {
        return (Brush)target.GetValue(MyNewBrushProperty);
    }

    public static void SetMyNewBrush(DependencyObject target, Brush value)
    {
        target.SetValue(MyNewBrushProperty, value);
    }
}

以及样式和模板中的用法:

<Style TargetType="{x:Type Button}">
    <Setter Property="local:CustomProps.MyNewBrush" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:CustomProps.MyNewBrush)}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用此方法仍然允许覆盖单个实例上的值。

The short answer is no. The ControlTemplate is essentially a black box, at least where XAML is concerned (there are ways to dig down the visual tree in code).

When you say you "can't use TemplateBinding", why not? If you just don't have enough available properties that can be fixed by creating some attached properties for the values you want to pass through. This is assuming you're templating a control that you can't change, otherwise you can just add new dependency properties.

Attached property:

public static class CustomProps
{
    public static readonly DependencyProperty MyNewBrushProperty = DependencyProperty.RegisterAttached(
        "MyNewBrush",
        typeof(Brush),
        typeof(CustomProps),
        new UIPropertyMetadata(Brushes.Green));

    public static Brush GetMyNewBrush(DependencyObject target)
    {
        return (Brush)target.GetValue(MyNewBrushProperty);
    }

    public static void SetMyNewBrush(DependencyObject target, Brush value)
    {
        target.SetValue(MyNewBrushProperty, value);
    }
}

And usage in Style and Template:

<Style TargetType="{x:Type Button}">
    <Setter Property="local:CustomProps.MyNewBrush" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:CustomProps.MyNewBrush)}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Using this method also still allows overriding values on individual instances.

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