WPF绑定到一个样式中另一个属性的绑定 [英] WPF binding to another property's binding in a style

查看:134
本文介绍了WPF绑定到一个样式中另一个属性的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定提出这个问题的最佳方式(对于含糊不清的问题标题抱歉),但本质上我想使用一个值转换器在TextBox中设置MaxLength属性,该值转换器是从数据上下文和传入属性中的属性作为转换器参数。我想以一种风格来做所有这些,而不是在逐个控制的基础上。这是以非风格的方式执行此操作的示例:

 < TextBox Text ={Binding MyPropertyName.TheirPropertyName} MaxLength ={Binding MyPropertyName,Converter = {StatocRespirceMyCoolConverter},ConverterParameter = TheirPropertyName}/> 

(如果您想知道,TheyPropertyName表示具有属性的MyPropertyName类型的属性像[StringMaxLength(15)],我可以得到并返回值转换器。)
另外,有什么办法传递类型的MyPropertyName而不是实例?我只需要类型来执行StringMaxLength属性查找。



无论如何,我可以如何在一个样式中做这样的事情?我已经得到:

 < Setter Property =MaxLength> 
< Setter.Value>
< Binding Converter ={StaticResource textFieldMaxLengthConverter}/>
< /Setter.Value>
< / Setter>

但是,将整体数据文本传递给值转换器,而不是MyPropertyName对象,而I真的没有线索,如果我可以解析绑定的MyPropertyName.TheirPropertyName部分,将绑定的ConverterParameter属性中传递TheyPropertyName。



任何指导都是真的赞赏!

解决方案

好的,经过一番挖掘,我已经弄清楚了这一点,令我满意。我绑定到RelativeSource Self,然后解析Text属性上的绑定表达式(因为这是一个TextFieldMaxLength转换器,我假定我正在反对一个TextBox。



资源字典中的样式:

 < Style TargetType =TextBox> 
&Set Property =MaxLength>
< Setter.Value>
< Binding Converter ={StaticResource textFieldMaxLengthConverter}RelativeSource ={RelativeSource Self}/>
& Setter.Value>
< / Setter>
< / Style>

使用(基本上没有什么特别需要做,因为它的风格):

 < TextBox Text = {Binding MyPropertyName.TheirPropertyName}/> 

TextFieldMaxLengthConverter的转换方法:

  public object Convert(object value,Type targetType,object parameter, System.Globalization.CultureInfo culture)
{
控制控件=值作为控件;
BindingExpression be = control.GetBindingExpression(TextBox.TextProperty);
if(be!= null)
{
string boundPropertyName = be.ParentBinding.Path.Path;
// .. boundPropertyName这里是MyPropertyName.TheirPropertyname,做一些解析,并返回一个值,基于
}
}

(显然,我的实际实现有点复杂/处理意外输入/根据我的原始问题的陈述使用反射)。



无论如何,以为我会发布这个解决方案,以防其他人尝试做类似的事情,或者如果可能比我使用的更好的方法。


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!

解决方案

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:

<Style TargetType="TextBox"> 
  <Setter Property="MaxLength">
    <Setter.Value>
      <Binding Converter="{StaticResource textFieldMaxLengthConverter}" RelativeSource="{RelativeSource Self}" />
    </Setter.Value>
  </Setter>
</Style>

The usage (basically showing nothing special needs to be done since it's all in the style):

<TextBox Text="{Binding MyPropertyName.TheirPropertyName}" />

The Convert Method for the textFieldMaxLengthConverter:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
  Control control = value as Control;
  BindingExpression be = control.GetBindingExpression(TextBox.TextProperty);
  if (be != null)
  {
    string boundPropertyName = be.ParentBinding.Path.Path;
    // .. boundPropertyName here is MyPropertyName.TheirPropertyname, do some parsing and return a value based on that
  }
}

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

这篇关于WPF绑定到一个样式中另一个属性的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆