views:

42

answers:

1

A templated control I'm working on uses a ValueConverter like so:

<ListBox>
   <ListBox.Resources>
      <Controls:CodeDescriptionValueConverter x:Key="CodeDescriptionValueConverter"/>
   </ListBox.Resources>
   <ListBox.ItemTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding Converter={StaticResource CodeDescriptionValueConverter}"/>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

This is the default look that I supply in generic.xaml. When I use this control I'll want to pass different format strings into the converter. Is there a way to make that happen without providing the full ControlTemplate?

My first thought was that I could use ConverterParameter with a TemplateBinding to a property on the control, but I discovered that ConverterParameters can't be bound to. Another option could be to get access to the control from the ConvertTo method, then pick off that property. I'm not sure how to do that. Any options that would eliminate the need to completely re-template the control each time I use it would be helpful (it's a lot of Xaml).

+1  A: 

In these situations, I generally do one of two things:

1) Bind to an object that has access to both the property you want to bind to the format string. In the the converter you will then have access to both the property and the format string.

2) Add properties to your data object/viewmodel/etc for the format string and the formatted text. Then bind to the formatted text properties. Assuming that you are using INotifyPropertyChanged, keep in mind that you will need to fire the propertychanged event for the formatted text property whenever you change the text or format string properties

Jacob Adams
Thanks for the feedback, those are some good possibilities that I hadn't thought of.
James Cadd