0
votes

I'm making a color selector combobox with checkboxes.

Combobox's visible Text is "Colors", and items are the names of colors with a checkbox.

Scenario: open combobox -> select whatever in comboboxItems -> get checked(Selected)items.

So, when the user clicks a combobox item, I want to change the checkbox value and keep the combobox opened.

I'm stuck with the checking (selecting) items functionality.

How to do this?

Model:

public class ColorItem : DependencyObject
{
    public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register
        ("Name", typeof(string), typeof(ColorItem),
        new PropertyMetadata(string.Empty));

    public static readonly DependencyProperty IsSelectedProperty =
        DependencyProperty.Register
        ("IsSelected", typeof(bool), typeof(ColorItem),
        new PropertyMetadata(true));

    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
    public bool IsSelected
    {
        get { return (bool)GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, value); }
    }
}

XAML:

<ComboBox Name="ColorCombobox" IsEditable="True" IsReadOnly="True" Focusable="False" StaysOpenOnEdit="True"
            ItemsSource="{Binding ColorItems}" SelectionChanged="OnComboboxSelectionChanged" Text="Colors" Margin="0,0,30,0" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}" Width="20" />
                <TextBlock Text="{Binding ColorName}" Width="100" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

SelectionChanged Event handler in code-behind:

private void OnComboboxSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    ComboBox box = sender as ComboBox;

    box.Text = "Colors"; //Not works. Text  will empty be "" 
}
1
What's your view model look like? Also if binding why are you handling an event in your view like that?zaggler
@Çöđěxěŕ i just tried to keep text to "Colors" display on SelectionChanged event but not works.박지석
Please see stackoverflow.com/questions/11429608/… which may be of help.zaggler
@박지석: What exactly is your issue here? What's not working the way you want it to?mm8
@mm8 i want to select(check) item on combobox. like this one : syncfusion.com/wpf-ui-controls/combobox박지석

1 Answers

3
votes

Get rid of the StackPanel and the TextBlock and define an ItemContainerStyle that stretches the content of the ComboBoxItem:

<ComboBox Name="ColorCombobox" IsEditable="True" IsReadOnly="True" Focusable="False" StaysOpenOnEdit="True"
          ItemsSource="{Binding ColorItems}" SelectionChanged="OnComboboxSelectionChanged" Text="Colors" Margin="0,0,30,0" >
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ComboBox.ItemContainerStyle>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>