Question

I need to allow selection of several items from this predefined list:

public enum QuarkType{
    Up,
    Down,
    [Description("Magical Being")] Charm,
    [Description("Quite Odd")] Strange,
    Top,
    Bottom
}

So I use CheckComboBox, and use the DescriptionAttribute where I need to use custom description. I feed the CheckComboBox using a MarkupExtension that returns a list of all values of the given enum as IEnumerable<EnumDescriptionPair>, where EnumDescriptionPair is:

public class EnumDescriptionPair{
    public object Value { get; set; }
    public string Description { get; set; }
}

Now the problem is how to pass the Values of this list to the code-behind list:

public ObservableCollection<QuarkType> SelectedQuarksList { get; set; }

I mean, how to take just the Value out of the EnumDescriptionPair for each item of the selected list ?

This is what I have thus far. It obviously doesn't work (meaning it shows the right strings in the CheckComboBox, and allows selecting several items, but isn't reflected in the SelectedQuarksList mentioned above):

<Window x:Class="MyEditor.MainWindow"
        xmlns:loc="clr-namespace:MyEditor"
        xmlns:toolKit="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <StackPanel>
        <toolKit:CheckComboBox x:Name="Ccb" Delimiter=","
                               ItemsSource="{loc:EnumItemsValueConverter {x:Type loc:QuarkType}}"
                               DisplayMemberPath="Description"
                               SelectedItemsOverride="{Binding SelectedQuarksList}" />

        <ListBox ItemsSource="{Binding SelectedQuarksList}" />
    </StackPanel>
</Window>

No correct solution

OTHER TIPS

To do exactly what your question asks, you could try using a converter on the SelectedQuarksList binding that does a ".Select(q => q.Value)" in the ConvertBack function.

To get the behavior you want, I have done this successfully in the past (example with 2 of your values), this sets up the enum as "Flags" so the value sequence goes 0, 1, 2, 4...:

<StackPanel Orientation="Horizontal">
<Checkbox Content="Up" IsChecked="{Binding Path=SelectedQuarksFlags, Converter={Static Resource HasFlagToBoolConverter}, ConverterParamater={x:Static Quarks.Up}}"
<Checkbox Content="Magical Being" IsChecked="{Binding Path=SelectedQuarksFlags, Converter={Static Resource HasFlagToBoolConverter}, ConverterParamater={x:Static Quarks.Charm}}"
</StackPanel>

The converter looks like:

 Quark _lastSeenValue;
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Quark paramQuark = (Quark)parameter;
        Quark currentQuark = (Quark)value;

        _lastSeenValue = currentQuark;
        return currentQuark.HasFlag(paramQuark);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Quark newQuark = _lastSeenValue;
        Quark paramQuark = (Quark)parameter;

        if ((bool)value)
        {
            newQuark |= paramQuark; 
        }
        else
        {
            newQuark &= ~paramQuark;
        }

        _lastSeenValue = newQuark;
        return newQuark;
    }

This could be converted to add or remove from a list relatively easily, but I know the code above works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top