我的 WPF 样式设置器可以使用 TemplateBinding 吗?

发布于 2024-08-03 11:50:10 字数 995 浏览 13 评论 0原文

我正在尝试做这样的事情......

<Style
    x:Key="MyBorderStyle"
    TargetType="Border">
    <Setter
        Property="Padding"
        Value="{TemplateBinding Padding}" />
</Style>

但我收到错误:

“Padding”成员无效,因为它没有限定类型名称。

如何提供“限定类型名称”?

注意:我尝试这样做的原因是我想在一系列类似的 ControlTemplate 中包含相同的边框。

我也尝试过这个:

<Setter
    Property="Padding"
    Value="{TemplateBinding GridViewColumnHeader.Padding}" />

...它实际上已编译,但是当我运行应用程序时,我得到了一个 XamlParseException

无法将属性“Value”中的值转换为类型“”的对象。

我想也许可以使用 GridViewColumnHeader (这是我的 ControlTemplate)来限定 Padding想要使用这种风格)会起作用,但没有骰子。

编辑:

嗯,根据 TemplateBinding 的文档,它说:

将控件模板中的属性值链接为模板化控件上某些其他公开属性的值。

所以听起来我想做的事情简直是不可能的。我真的希望能够为我的控件模板中的某些控件创建可重用的样式,但我想模板绑定不能包含在这些样式中。

I'm trying to do something like this...

<Style
    x:Key="MyBorderStyle"
    TargetType="Border">
    <Setter
        Property="Padding"
        Value="{TemplateBinding Padding}" />
</Style>

...but I get the error:

'Padding' member is not valid because it does not have a qualifying type name.

How do I provide a "qualifying type name"?

Note: The reason I'm trying to do this, is that I'd like to include the same Border in a series of similar ControlTemplates.

I also tried this:

<Setter
    Property="Padding"
    Value="{TemplateBinding GridViewColumnHeader.Padding}" />

...and it actually compiled, but then when I ran the app, I got a XamlParseException:

Cannot convert the value in attribute 'Value' to object of type ''.

I thought maybe qualifying Padding with GridViewColumnHeader (which is the ControlTemplate I want to use this style with) would work, but no dice.

EDIT:

Well, according to the documentation for TemplateBinding, it says:

Links the value of a property in a control template to be the value of some other exposed property on the templated control.

So it sounds like what I'm trying to do is just plain impossible. I really would like to be able create reusable styles for certain controls in my control templates, but I guess the template bindings cannot be included in these styles.

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

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

发布评论

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

评论(3

肩上的翅膀 2024-08-10 11:50:10

TemplateBinding 应该适用于您正在对控件进行模板化并且希望将该控件的属性值绑定到模板内其他控件的属性的情况。在您的情况下,您正在模板化某些内容(将其称为 MyControl),并且该模板将包含一个边框,其 Padding 应绑定到 MyControl 的填充。

来自 MSDN 文档

TemplateBinding 是针对模板场景的 Binding 的优化形式,类似于使用 {BindingrelativeSource={RelativeSource TemplatedParent}} 构造的 Binding。

但无论出于何种原因,指定 TemplatedParent 作为绑定源似乎在样式设置器中不起作用。为了解决这个问题,您可以将相对父级指定为正在模板化的控件的 AncestorType(如果您没有在 MyControl 模板中嵌入其他 MyControl,则可以有效地找到 TemplatedParent)。

当我尝试自定义按钮控件模板时,我使用了此解决方案,其中按钮的(字符串)内容需要绑定到按钮的 ControlTemplate 中 TextBlock 的 Text 属性。该代码如下所示:

<StackPanel>
    <StackPanel.Resources>
        <ControlTemplate x:Key="BarButton" TargetType="{x:Type Button}">
            <ControlTemplate.Resources>
                <Style TargetType="TextBlock" x:Key="ButtonLabel">
                    <Setter Property="Text" Value="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type Button}} }" />
                </Style>
            </ControlTemplate.Resources>
            <Grid>
                <!-- Other controls here -->
                <TextBlock Name="LabelText" Style="{StaticResource ButtonLabel}" />
            </Grid>
        </ControlTemplate>
    </StackPanel.Resources>
    <Button Width="100" Content="Label Text Here" Template="{StaticResource BarButton}" />
</StackPanel>

TemplateBinding should work for the case where you're templating a control and you want to bind the value of a property of that control to a property of a different control inside the template. In your case you're templating something (call it MyControl), and that template will include a border whose Padding should be bound to MyControl's padding.

From MSDN documentation:

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}.

But for whatever reason, specifying TemplatedParent as the source for the binding doesn't seem to work within Style Setters. To get around that you can specify the relative parent to be an AncestorType of the control you're templating (which effectively finds the TemplatedParent providing you haven't embedded other MyControls in the MyControl template).

I used this solution when I was trying to custom template a Button control in which the (String) Content of the Button needed to be bound to the Text property of a TextBlock in the ControlTemplate for the button. Here's what that code looked like:

<StackPanel>
    <StackPanel.Resources>
        <ControlTemplate x:Key="BarButton" TargetType="{x:Type Button}">
            <ControlTemplate.Resources>
                <Style TargetType="TextBlock" x:Key="ButtonLabel">
                    <Setter Property="Text" Value="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type Button}} }" />
                </Style>
            </ControlTemplate.Resources>
            <Grid>
                <!-- Other controls here -->
                <TextBlock Name="LabelText" Style="{StaticResource ButtonLabel}" />
            </Grid>
        </ControlTemplate>
    </StackPanel.Resources>
    <Button Width="100" Content="Label Text Here" Template="{StaticResource BarButton}" />
</StackPanel>
零度° 2024-08-10 11:50:10

{TemplateBinding ...} 快捷方式在 Setter 中不可用。

但没有人会阻止您使用完整的详细版本,例如:

Value="{BindingrelativeSource={RelativeSource TemplatedParent}, Path=Padding}"

The {TemplateBinding ...} shortcut is not available in a Setter.

But nobody will stop you using the full verbose version such as:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Padding}".

死开点丶别碍眼 2024-08-10 11:50:10

只需在属性前面加上类型名称前缀即可对属性进行限定。例如,使用 Border.Padding 而不是 Padding

但是,我不确定这对您的场景是否有意义。 TemplateBinding 用于控件模板内部。

A property can be qualified simply by prefixing it with the type name. For example, Border.Padding instead of Padding.

However, I'm not sure it makes sense for your scenario. TemplateBindings are used inside a control template.

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