0

I am hoping someone can help me out with this. I have asked a similar question before but I didn't have anything started on this at the time. I have found the SO question link

that is similar to my problem but it has one issue. The combobox doesn't show the selected enums. I do the example in the link working in my sample app but I have no clue how to get the Combobox's text to show the currently selected item(s). Anyone have a suggestion on what to do? I am real stuck on this one.

Here is my current combobox:

<ComboBox>
    <CheckBox Content="SAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SAW}}" />
    <CheckBox Content="FCAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.FCAW}}" />
    <CheckBox Content="SMAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SMAW}}" />
</ComboBox>

My Converter is:

public class WeldingProcessFlagValueConverter : IValueConverter
{
    private WeldingProcess target;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        WeldingProcess mask = (WeldingProcess)parameter;
        this.target = (WeldingProcess)value;
        return ((mask & this.target) != 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        this.target ^= (WeldingProcess)parameter;
        return this.target;
    }
}

So my 'CurWeldingProcess' is showing the correct value when I select any combination of the checkboxes, but I dont know how to get the combobox to show the selected values ('CurWeldingProcess'). Any ideas?

Community
  • 1
  • 1
Travyguy9
  • 4,774
  • 8
  • 43
  • 63

1 Answers1

2

If you need to show a "concatenation" of the selected items (i.e. if I check SAW and SMAW enum values, I would like to see in the ComboBox Text something like "SAW, SMAW"), you can take a look to this Multi Select ComboBox in WPF.

You will find both a MVVM version and "codebehind" one.

EDIT

Ok, you can go the CodeProject and download the MultiSelectComboBox dll. Add it in your project. Then in your XAML you can add:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:multi="clr-namespace:MultiSelectComboBox;assembly=MultiSelectComboBox"
        Title="MainWindow" Height="350" Width="600">
    <!-- your xaml -->
        <multi:MultiSelectComboBox Margin="4" Name="MultiSelectComboBox" />
    <!-- the rest of your xaml -->
</Window>

Then in your code-behind (I used for my sample the TextAlignment enum):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Dictionary<string, object> itemsSource = new Dictionary<string, object>();
        itemsSource.Add(Convert.ToString(TextAlignment.Center), TextAlignment.Center);
        itemsSource.Add(Convert.ToString(TextAlignment.Justify), TextAlignment.Justify);
        itemsSource.Add(Convert.ToString(TextAlignment.Left), TextAlignment.Left);
        itemsSource.Add(Convert.ToString(TextAlignment.Right), TextAlignment.Right);

        MultiSelectComboBox.ItemsSource = itemsSource;
    }
}

The SelectedItems property of the MultiSelectComboBox will contain the values that the user selected. Is it what you needed? If you are using MVVM you can expose the ItemsSource and the SelectedItems dictionaries with your ViewModel.

Il Vic
  • 5,576
  • 4
  • 26
  • 37
  • That one doesn't use enums though. How do I have it use the enums? – Travyguy9 Apr 18 '15 at 19:43
  • @Travyguy9 I have just edited my answer, I hope it can help you. – Il Vic Apr 20 '15 at 13:45
  • This solution works. I am not a HUGE fan of having to keep two lists and managing the Flags enum manually but it does work. Doesn't appear someone will come with a different solution so you get the accepted answer. Thanks for the help! – Travyguy9 Apr 21 '15 at 14:34