views:

46

answers:

2

I have a form that is generated based on several DataTemplate elements. One of the DataTemplate elements creates a TextBox out of a class that looks like this:

public class MyTextBoxClass
{
   public object Value { get;set;}
   //other properties left out for brevity's sake
   public string FormatString { get;set;}
}

I need a way to "bind" the value in the FormatString property to the "StringFormat" property of the binding. So far I have:

<DataTemplate DataType="{x:Type vm:MyTextBoxClass}">
 <TextBox Text="{Binding Path=Value, StringFormat={Binding Path=FormatString}" />
</DataTemplate>

However, since StringFormat isn't a dependency property, I cannot bind to it.

My next thought was to create a value converter and pass the FormatString property's value in on the ConverterParameter, but I ran into the same problem -- ConverterParameter isn't a DependencyProperty.

So, now I turn to you, SO. How do I dynamically set the StringFormat of a binding; more specifically, on a TextBox?

I would prefer to let XAML do the work for me so I can avoid playing with code-behind. I'm using the MVVM pattern and would like to keep the boundaries between view-model and view as un-blurred as possible.

Thanks!

+1  A: 

One way may be to create a class that inherits TextBox and in that class create your own dependency property that delegates to StringFormat when set. So instead of using TextBox in your XAML you will use the inherited textbox and set your own dependency property in the binding.

Jakob Christensen
That is a good suggestion. I'll have to look into this. I was kind of hoping there would be a solution that didn't involve custom controls, but I'm certainly open to it. I'll check back after a little research.
Jason Williams
A: 

Just bind the textbox to the instance of a MyTextBoxClass instead of MyTextBoxClass.Value and use a valueconverter to create a string from the value and formatstring.

Another solution is to use a multivalue converter which would bind to both Value and FormatString.

The first solution don't support changes to properties, that is if value or formatstring changes the value converter will not be called like it would be if you are using a multivalueconverter and binding directly to the properties.

Wallstreet Programmer
Binding to the MyTextBoxClass instance is something I tried, but the ConvertBack method in the ValueConverter is going to be a problem since there are many, many properties that I don't have a place for on a TextBox object. So, I would get an incomplete object coming back from the TextBox.I'll look into the multi value converter. However, FormatString isn't bindable since it is a dependency property, so I'm not sure that's going to work.
Jason Williams
How is this supposed to work? When the TextBox is updated using databinding the text is formatted using the FormatString. When a user updates the textbox he can enter any text which may be inconsistent with the formatting of the FormatString. Is that OK? Are you sure you don't want to use a masked textbox instead? Also, FormatString is as bindable as any other public property.
Wallstreet Programmer