WPF binding to another property's binding in a style

后端 未结 2 1599
情歌与酒
情歌与酒 2021-01-15 08:43

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

相关标签:
2条回答
  • 2021-01-15 09:12

    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.

      <Setter
         Property="MaxLength">
         <Setter.Value>
            <Binding
               Converter="{StaticResource textFieldMaxLengthConverter}"
               RelativeSource="{RelativeSource TemplatedParent}"
               Path="MyPropertyName"
               ConverterParameter="TheirPropertyName" />
         </Setter.Value>
      </Setter>
    
    0 讨论(0)
  • 2021-01-15 09:14

    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.

    0 讨论(0)
提交回复
热议问题