-1

In WPF, is it possible to pass the name/path of the binding as a parameter? For example, say I have this:

<TextBox Width="300" Text="{Binding Path=Age, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True, Converter={x:Static local:AgeConverter.Instance}, ConverterParameter={Binding ???}}" />

Is there anything I can put instead of the ??? so that the parameter value is "Age"?

Gigi
  • 28,163
  • 29
  • 106
  • 188
  • 2
    possible duplicate of [Binding ConverterParameter](http://stackoverflow.com/questions/15309008/binding-converterparameter) – Alessandro Rossi Jun 20 '14 at 12:25
  • What does that have to do with it? Read the question. – Gigi Jun 20 '14 at 12:26
  • 1
    Could the phrase "The ConverterParameter property is not a dependency property and hence can not be bound." tell you something about it? – Alessandro Rossi Jun 20 '14 at 12:28
  • To make parameter value "Age" you should put ConverterParameter=Age. If you want something different, see Alessandro's link in first comment. – icebat Jun 20 '14 at 12:30
  • 1
    *What does that have to do with it? Read the question.*... wow, you're rude for someone who is asking for help. – Sheridan Jun 20 '14 at 12:51

1 Answers1

0

As @Alessandro mentionned, the ConverterParameter is not a DependencyProperty so it does't support data binding.

But since the ConverterParameter is a regular property of the Binding class, so you can set its value programmatically. You can do this in code:

var myTextBox = new TextBox();
Binding myBinding = new Binding("Age");
myBinding.ConverterParameter = "set this to anything you want";
myTextBox.SetBinding(TextBlock.TextProperty, myBinding);

For example, you could change the value of ConverterParameter at a later time in an event handler to react to user input. The next time the binding is updated, it will call the Converter with the new ConverterParameter.

private void Button_Click(object sender, RoutedEventArgs e)
{
    myBinding.ConverterParameter = "a different converter parameter";
}
bouvierr
  • 3,563
  • 3
  • 27
  • 32