Output ComboBox SelectedValuePath from WPF

In our application, we have a very large data set that acts as our data dictionary for ComboBox lists, etc. This data is statically cached and disconnected from two variables, so I found it reasonable to write a control that was obtained from ComboBox and exposed the two keys as DP. When these 2 keys have the correct values, I automatically install the ItemsSource from the ComboBox from the list of data dictionary to which it corresponds. I also automatically set SelectedValuePath and DisplayMemberPath in the constructor in Code and Description respectively.

The following is an example of how an item in an ItemsSource from a list of data dictionaries always looks:

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

The code value always has a length of 4 characters, but sometimes I just need to associate 3 characters. Therefore, the Code3 property.

Here's how the code looks inside my custom combobox for setting 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 that when I change SelectedValuePath in XAML to Code3, this does not work. What I associate with SelectedValue still gets the full 4-character code from the DataDictionaryItem. I even tried restarting SetItemsSource when SelectedValuePath was changed and there were no cubes.

Can anyone see what I need to do to get my own custom combobox to wake up and use SelectedValuePath if it is overridden in XAML? Fine tuning the value in setting properties in my associated SelectedValue business object is not an option.

, XAML combobox :

<c:CustomComboBox Key1="0" Key2="8099" SelectedValuePath="Code3" SelectedValue="{Binding Thing}"/>

: snoop , , SelectedValuePath - ... Code3... Zuh?

+3
1

, .

-, DependencyProperty WPF no-no. , :

static ValueCodeListComboBox()
{
  SelectedValuePathProperty.OverrideMetadata(typeof(ValueCodeListComboBox), new PropertyMetadata("Code"));
  DisplayMemberPathProperty.OverrideMetadata(typeof(ValueCodeListComboBox), new PropertyMetadata("Description"));
}

:

.

, , FrameworkPropertyMetadata PropertyMetadata:

static ValueCodeListComboBox()
{
  SelectedValuePathProperty.OverrideMetadata(typeof(ValueCodeListComboBox), new FrameworkPropertyMetadata("Code"));
  DisplayMemberPathProperty.OverrideMetadata(typeof(ValueCodeListComboBox), new FrameworkPropertyMetadata("Description"));
}

SelectedValuePath XAML .

+4
source

Source: https://habr.com/ru/post/1736029/


All Articles