How can I bind a ConverterParameter value ?

I would like to make a textblock display the remaining time of an operation. The bound object has two properties: ElapsedTime and TotalDuration, both of type TimeSpan.

I thought the following should work, but it doesn't because the parameter passed to my custom converter is a Binding object, instead of the result of the binding:

<TextBlock
Text="{Binding ElapsedTime,
Mode=OneWay,
ConverterParameter={Binding TotalDuration},
Converter={StaticResource RemainingTimeConverter}}" />


The RemainingTimeConverter is implemented to subtract the "value" from the "parameter" and return a string representation of the resultant TimeSpan value.


Why isn't the inner Binding evaluated and its value passed to the outter binding ConverterParameter Is this by design If that's the case what's the use of having ConverterParameter if I can only use harcoded values





Answer this question

How can I bind a ConverterParameter value ?

  • brk

    I was trying to somehow get that

    I was able to access a property in the customcontrol like this

    <Binding Path="prop1" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl1}}"></Binding>

    but the for another property, I need to multiply that with another property in the control

    that is where I am having issues



  • NeoBugs

    If you are writing a type converter and, like me, you are unable to pass a runtime parameter to that converter then you just increasing the coupling between the converter and the value that you want to pass. For example, suppose you can't pass a parameter to a function (for some reason). Then, instead of passing the parameter you could create a global variable, a public static property or a member variable and access it directly from the function. The function would not be generic anymore, instead very coupled, but it would solve the problem (and probably create others) . The solution someone suggested above is pretty much like that but in XAML. I am looking for the correct solution.

    I am not very familiar with MarkupExtensions. However, I believe it may be possible to write one to replace "Binding" where we could solve this problem in a more appropriate manner.





  • _Andrew

    how did you solve this

    can you give me an example

    Thanks



  • andrea loleo

    My situation is I have a customcontrol and in the that I am using a converter which needs a parameter .this will have the value with which the data value will get multiplied, this will not be known at the design time and will be calculated in codebehind.

  • asmo

    The answer is straight-forward, but not what you want to hear.

    You can only target a binding at DependencyProperty on a DependencyObject. Binding does not inherit from DO, so you can't binding the converter parameter.

    If you want other state passed into a converter, you may have to subclass the desired obect and add new properties (as has been suggested).



  • Martin Stein

    my mistake, the second one is not property on the control it is bound to a data ( a data element in the bound data)



  • Gerard van Soest

    Does anyone know if binding inside bindings are supported

  • mancha

    Why not create a class that would contain your information

    Then you instantiate your class with XAML, and pass it as a parameter.

    create class MyTimeDuration with property for TotalDuration.

    in XAML you add in resources:
    <(namespace of class):MyTimeDuration x:Key="MyDuration">

    Then you pass the key as the ConvertParameter like:

    ConvertParameter={StaticResource MyDuration}

    You can also add public functions to reset your duration such that you can bind it to a button.

    Hope this helps.

  • Geeks

    Yes, I could instantiate a helper class in XAML just to do that but that would be a workaround and not elegant. I have also found other tricks I could do but I am looking for the appropriate solution since the goal of the project I am working on is just to learn WPF.

    It would be very strange to be limited to XAML hardcoded values (and resources) when setting the ConvertParameter property, don't you agree


  • Kalle B

    ... or use MultiBinding

  • Lusty_Learner

    You're problem is more complicated than mine. Let's hope someone from Microsoft can give us a definite answer on how to pass bound parameters to converters. Meanwhile you may want to investigate MarkupExtensions.

  • bongolas

    If you want to multiply a property by another property in the same instance, why don't you create a third property that multiplies the first two and just bind to that property


  • DragonVic

    You have to figure out a way to pass the instance of the custom control as a parameter to the converter. Then, inside, the converter access the property on that instance.

    I don't know how you can pass an instance of the custom control in XAML. Maybe you can pass a StaticResource which in turn could povide the instance ...

    What is strange for me is that it's easy to assign a Name to the objects we declare in XAML. However, I have never seem a way to refer to those instances by the Name in XAML. Isn't this a design oversight


  • Calvin Thomas

    Could someone help me with this

    Thanks



  • How can I bind a ConverterParameter value ?