0

I'm stuck in this problem. I have a combobox in my application (created in code, not XAML); I have populated it with checkboxes, because I needed a drop-down control with checkboxes inside.

Now, I don't need the combobox selection, because it's meaningless. So I wanted to show, in the text part of the control, a label.

Is there a way to do this? Here is a minimal example:

myComboBox = new System.Windows.Controls.ComboBox();

foreach (var key in myDictionary.Keys)
{
    System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox();
    chk.Content = key;
    chk.SetBinding(System.Windows.Controls.CheckBox.IsCheckedProperty, new Binding() { Mode = BindingMode.TwoWay, Source = this, Path = new PropertyPath("myDictionary[" + key + "]") });

    RoutedEventHandler ev = (sender, e) =>
    {
        // Do something when a checkbox is changed
    };
    chk.Checked += ev;
    chk.Unchecked += ev;

    myComboBox.Items.Add(chk);
}

This way if the user clicks on a checkbox the checkbox content is displayed in the text field.

I modified it adding also

myComboBox.SelectionChanged += (sender, jender) =>
{
    myComboBox.SelectedItem = null;
};

This way no text is ever displayed. But.. What if I wanted to write a fixed string inside the text part of the combobox?

Thank you

frarugi87
  • 2,826
  • 1
  • 20
  • 41
  • Have you considered using the WPF Toolkit? They have a control which is exactly what you describe: https://wpftoolkit.codeplex.com/wikipage?title=CheckComboBox Otherwise, you could use ItemTemplate I believe. – Joe Apr 18 '16 at 13:26
  • @Joe Thank you for your comment. Anyway I've never considered the WPF toolkit; thanks. That control, however, concatenates the checkboxes content together; I needed a fixed string to appear there in place of the checkboxes content.. – frarugi87 Apr 18 '16 at 13:35
  • Ah, sorry I thought that was what you were after. – Joe Apr 18 '16 at 14:00

1 Answers1

2

Wow, this is a real pain. I assumed that it would be pretty simple but it turns out it isn't. I assumed that you could set a template for the content displayed in the combobox, and separately for the content displayed in the drop down list. You can, but you have to use a content selector:

How to display a different value for dropdown list values/selected item in a WPF ComboBox?

This is the proper way of doing it. However, are you just wanting a straight up static label/string displayed? If so, it's probably much, much easier to just overlay that over what you've already got in a grid like this:

    <Grid>
        <ComboBox x:Name="Checkbox" SelectionChanged="Checkbox_SelectionChanged">
            <ComboBox.Items>
                <CheckBox Content="Test1"/>
                <CheckBox Content="Test2"/>
                <CheckBox Content="Test3"/>
            </ComboBox.Items>
        </ComboBox>
        <TextBlock IsHitTestVisible="False" Text="My Text" VerticalAlignment="Center" Margin="5"/>
    </Grid>

Setting the textblock invisible to hit-tests means they're just passed down to the combobox:

enter image description here

Community
  • 1
  • 1
Joe
  • 6,773
  • 2
  • 47
  • 81
  • Thank you for your help. This was another solution I tried but did not know about the hit test, so I discarded it ;) In the end I came up adding another element in the combobox (with `myComboBox.Items.Add("My Text");` before the `foreach`). It displays another element, but since it is in a different color it looks like a heading... Anyway I'll keep this solution in mind – frarugi87 Apr 18 '16 at 14:46