我的 WPF 样式设置器可以使用模板绑定吗?

编程入门 行业动态 更新时间:2024-10-25 21:23:56
本文介绍了我的 WPF 样式设置器可以使用模板绑定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

<Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="BorderBrush" Value="{StaticResource MyBorderBrush}" /> <Setter Property="Background" Value="{StaticResource MyBackgroundBrush}" /> <Setter Property="Padding" Value="{TemplateBinding Padding}" /> </Style>

...但我收到错误消息:'Padding' 成员无效,因为它没有合格的类型名称.

...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"?

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

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.

谢谢.

嗯,我试过了……

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

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

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

无法将属性Value"中的值转换为类型"的对象.

我认为用 GridViewColumnHeader(这是我想使用这种样式的 ControlTemplate)来限定 Padding 可能会起作用,但没有骰子.

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

编辑 2:

好吧,根据 TemplateBinding 的文档,它说:

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.

推荐答案

这应该适用于您正在模板化控件并且希望将该控件的属性值绑定到不同控件的属性的情况模板里面.在您的情况下,您正在模板化某些内容(称为 MyControl),并且该模板将包含一个边框,其 Padding 应绑定到 MyControl 的 padding.

This 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.

来自 MSDN 文档:

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

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

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

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).

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

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>

更多推荐

我的 WPF 样式设置器可以使用模板绑定吗?

本文发布于:2023-11-06 14:39:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   绑定   样式   模板   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!