1

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??

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user2155957
  • 21
  • 1
  • 3
  • Any particular reason you are using an `EnumerableRowCollection`, and not a `Dictionary`? I know if you use a `Dictionary`, you can bind using `SelectedValuePath="Key" DisplayMemberPath="Value"` – Bob. Apr 04 '13 at 19:32

2 Answers2

3

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;
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Lee Louviere
  • 5,162
  • 30
  • 54
  • I still do not know what to write in the converter. Can I get the int value for the description in another way than just take again the whole list from the DB? There must be a simplest way of doing this after I already have the combobox that has the item source set to the list from the DB. Can I use the combobox's itemSource in the converter? – user2155957 Apr 06 '13 at 20:09
  • Convert from int value to description? Honestly, at this point you should consider [writing an enum and giving it display text](http://stackoverflow.com/questions/1331487/how-to-have-userfriendly-names-for-enumerations). If you set your enum to derive from int, you can cast directly from the db value into the enum, bind the enum to display and your converter just returns the display text attribute. – Lee Louviere Apr 09 '13 at 14:46
0

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}}).

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user2155957
  • 21
  • 1
  • 3