WPF 绑定到样式中另一个属性的绑定
我不确定提出这个问题的最佳方式(抱歉问题标题含糊不清),但本质上我想使用从数据上下文传入属性的值转换器来设置 TextBox 上的 MaxLength 属性,以及传入属性上的属性作为转换器参数。我想以一种风格来完成这一切,而不是在逐个控制的基础上。下面是一个以非样式化方式执行此操作的示例:(
<TextBox Text="{Binding MyPropertyName.TheirPropertyName}" MaxLength="{Binding MyPropertyName, Converter={StatocRespirceMyCoolConverter}, ConverterParameter=TheirPropertyName}" />
如果您想知道, TheirPropertyName 表示 MyPropertyName 类型的属性,该属性具有类似 [StringMaxLength(15)] 的属性,我可以获取该属性到值转换器并返回。) 此外,是否有任何方法可以传递 MyPropertyName 的类型而不是实例?我只需要类型来执行 StringMaxLength 属性查找。
不管怎样,我怎样才能以一种风格来做这样的事情呢?我已经了解了:
<Setter Property="MaxLength">
<Setter.Value>
<Binding Converter="{StaticResource textFieldMaxLengthConverter}" />
</Setter.Value>
</Setter>
但是它将整个数据上下文传递到值转换器,而不是 MyPropertyName 对象,而且我真的不知道是否可以让它解析绑定的 MyPropertyName.TheirPropertyName 部分以传递 TheirPropertyName在绑定的 ConverterParameter 属性上。
任何指导将非常感激!
I'm not sure the best way to ask this question (sorry for the ambiguous question title), but essentially I'd like to set the MaxLength property on a TextBox using a value converter that is passed in a property from the data context, and the property on the passed-in property as the converter parameter. I'd like to do all this in a style, as opposed to on a control-by-control basis. Here's an example of doing this in a non-styled manner:
<TextBox Text="{Binding MyPropertyName.TheirPropertyName}" MaxLength="{Binding MyPropertyName, Converter={StatocRespirceMyCoolConverter}, ConverterParameter=TheirPropertyName}" />
(In case you're wondering, TheirPropertyName represents a property on the type of MyPropertyName that has an attribute like [StringMaxLength(15)], which I'd be able to get to and return inside the value converter.)
Additionally, is there any way to pass in the type of MyPropertyName as opposed to the instance? I only need the type to do the StringMaxLength attribute lookup.
Anyway, how could I go about doing something like this in a style? I've gotten as far as:
<Setter Property="MaxLength">
<Setter.Value>
<Binding Converter="{StaticResource textFieldMaxLengthConverter}" />
</Setter.Value>
</Setter>
But that passes the overall datacontext in to the value converter, as opposed to the MyPropertyName object, and I really have no clue if I can have it parse the MyPropertyName.TheirPropertyName part of the binding to pass TheirPropertyName in on the ConverterParameter attribute of the binding.
Any guidance would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,经过更多的挖掘,我已经找到了令我满意的答案。我绑定到RelativeSource Self,然后解析Text属性上的绑定表达式(因为这是一个TextFieldMaxLength转换器,我假设我正在针对TextBox工作。
资源字典中的样式:
用法(基本上什么也不显示)特殊需要完成,因为它都是在样式中):
textFieldMaxLengthConverter 的转换方法:(
显然我的实际实现有点复杂/处理意外输入/根据我原来的问题的陈述使用反射)
。发布此解决方案,以防其他人尝试做类似的事情,或者可能有比我正在使用的更好的方法。
Ok, after some more digging, I've figured this out to my satisfaction. I'm binding to RelativeSource Self and then parsing the binding expression on the Text property (since this is a TextFieldMaxLength converter, I am presuming I'm working against a TextBox.
The styling up in the resource dictionary:
The usage (basically showing nothing special needs to be done since it's all in the style):
The Convert Method for the textFieldMaxLengthConverter:
(Obviously my actual implementation is a bit more complex/handles unexpected input/uses reflection as per my original question's statement).
Anyway, thought I would post this solution in case anyone else tries to do something similar, or if there might be a better way to do this than I am using.
您可以使用多重绑定将 lutiple 属性传递给转换器,这允许您根据需要对尽可能多的属性进行绑定,并且如果任何属性发生更改(即实现 INotifyPropertyChanged),则将重新评估绑定。对于您正在做的事情,您必须使用反射来查找传入对象上的属性,该属性具有与转换器参数匹配的特定属性名称。我认为您最终不会使用下面的代码,但它表明您可以在 xaml 中的绑定中使用多个参数。包括路径、转换器、转换器参数。我不确定相对来源,但我认为你可能需要它来做你想做的事。查看调试数据绑定以获得调试的好方法。这项技术是必不可少的。我不断地使用它。
you can pass in lutiple properties to your converter by using a multi binding, this allows you to do a binding on as may properties as you want, and if any of the properties change (i.e. implent INotifyPropertyChanged) the binding will be reevaluated. for what you are doing you would have to use reflection to find a property on the passed in object with a particular property name that matches your converter parameter. i dont think you will end up using the code below, but it shows you can have multiple parameters to your binding in xaml. including the path, converter, converter parameter. Im not sure about the relative source but however, but i think you might need it to do what you want. have a look at debugging Data Bindings for a good way to debug. this technique is essential. i use it continually.