1
votes

I have a ComboBox that is bound to an EnumerableRowCollection<T> :

ComboFamilyStatus.ItemsSource = EnumerableRowCollection<TaxDataSet.SourcesOfValuesRow> coll;

My xaml lookes like this:

<ComboBox Name="ComboFamilyStatus" DisplayMemberPath="Description" 
          Text="{Binding FamilyStatus, Converter={StaticResource FamilyStatusStringConverter}}">

I'm using the DisplayMemberPath to show the description of the row. The SourcesOfValuesRow has a value and a description and in the combo I want to see the description text. The Text is bound to the database where the FamilyStatus is saved as an int value this is why I added a converter.

My question is if the converter could convert from the int value to the string using the itemsource from the combobox? I don't see that the converter knows anything about the combo. In the meantime I wrote the converter to take again the EnumerableRowCollection<TaxDataSet.SourcesOfValuesRow> from the database and find there the matched description - this can't be the simplest way to do this! Any suggestions??

2
Any particular reason you are using an EnumerableRowCollection<T>, and not a Dictionary<int, string>? I know if you use a Dictionary<T1, T2>, you can bind using SelectedValuePath="Key" DisplayMemberPath="Value" - Bob.

2 Answers

3
votes

In this case, you're better off using a DataTemplate, instead of a Converter.

You already have a data class. Just use a DataTemplate that inserts a Textblock bound to the int value, then apply your converter there.

<ComboBox>
   <ComboBox.ItemTemplate>
      <DataTemplate DataType="{x:Type local:TaxDataSet.SourcesOfValuesRow}">
         <TextBlock Text="{Binding FamilyStatus, Converter={StaticResource FamilyStatusStringConverter}}"/>
      </DataTemplate>
   </ComboBox.ItemTemplate>
<ComboBox>

Change your SourcesOfValuesRow FamilyStatusProperty to an enum. Deriving from int lets you cast it directly.

enum FamilyStatusValues : int
{
   [Description("Married")]
   Married,
   [Description("Divorced")]
   Divorced,
   [Description("Living Together")]
   LivingTogether
}

Then in your converter use this code

ConvertTo(object value, ...)
{
   FieldInfo field = value.GetType().GetField(value.ToString());
   object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true));
   if(attribs.Length > 0)
   {
       return ((DescriptionAttribute)attribs[0]).Description;
   }
   return string.Empty;
}
0
votes

There is no need to use any converter. It worked for me using this:

<ComboBox Name="FamilyStatus" Grid.Row="7" Grid.Column="1" ItemsSource="{Binding Source={StaticResource comboProvider}}"
            SelectedValuePath="Value" DisplayMemberPath="Description" SelectedValue="{Binding FamilyStatus}">

Where DisplayMemberPath is the string from the TaxDataSet.SourcesOfValuesRow and SelectedValuePath is the int value. The SelectedValue is the value from the contact table (instead of writing in the combo Text="{Binding FamilyStatus, Converter={StaticResource FamilyStatusStringConverter}}).