WPF derived ComboBox SelectedValuePath issue

Posted by opedog on Stack Overflow See other posts from Stack Overflow or by opedog
Published on 2010-03-08T23:02:41Z Indexed on 2010/03/08 23:06 UTC
Read the original article Hit count: 1027

Filed under:
|
|

In our application we have a very large set of data that acts as our data dictionary for ComboBox lists, etc. This data is staticly cached and keyed off of 2 variables, so I thought it wise to write a control that derived from ComboBox and exposed the 2 keys as DPs. When those 2 keys have proper values I set the ItemsSource of the ComboBox automatically from the data dictionary list it corresponds to. I also automatically set the SelectedValuePath and DisplayMemberPath to Code and Description, respectively.

Here's how an example of how an item in the ItemsSource from the data dictionary list always looks:

public class DataDictionaryItem
{
  public string Code { get; set; }
  public string Description { get; set; }
  public string Code3 { get { return this.Get.Substring(0, 3); } }
}

The value of Code is always 4 bytes long, but sometimes I only need to bind 3 bytes of it. Hence, the Code3 property.

Here's how the code looks inside my custom combobox to set the ItemsSource:

private static void SetItemsSource(CustomComboBox combo)
{
  if (string.IsNullOrEmpty(combo.Key1) || string.IsNullOrEmpty(combo.Key2))
  {
    combo.ItemsSource = null;
    return;
  }

  List<DataDictionaryItem> list = GetDataDictionaryList(combo.Key1, combo.Key2);
  combo.ItemsSource = list;
}

Now, my problem is, when I change the SelectedValuePath in the XAML to Code3, it doesn't work. What I bind to SelectedValue still gets the full 4 character Code from DataDictionaryItem. I even tried rerunning SetItemsSource when the SelectedValuePath was changed and no dice.

Can anyone see what I need to do to get my custom combobox to wake up and use the SelectedValuePath provided if it's overridden in the XAML? Tweaking the value in the property setter in my SelectedValue bound business object is not an option.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about combobox