简体   繁体   English

如何使用复选框继续显示 Combobox 中的选定文本?

[英]How to keep displaying selected Text in Combobox with Checkbox?

I'm making a color selector combobox with checkboxes.我正在制作带有复选框的颜色选择器 combobox。

Combobox's visible Text is "Colors", and items are the names of colors with a checkbox. Combobox 的可见文本是“颜色”,项目是带有复选框的 colors 的名称。

Scenario: open combobox -> select whatever in comboboxItems -> get checked(Selected)items.场景:打开 combobox -> select 在组合框项目中的任何内容 -> 获取选中(选定)项目。

So, when the user clicks a combobox item, I want to change the checkbox value and keep the combobox opened.因此,当用户单击 combobox 项目时,我想更改复选框值并保持 combobox 处于打开状态。

I'm stuck with the checking (selecting) items functionality.我坚持检查(选择)项目功能。

How to do this?这个怎么做?

Model: 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: 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:代码隐藏中的 SelectionChanged 事件处理程序:

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

    box.Text = "Colors"; //Not works. Text  will empty be "" 
}

Get rid of the StackPanel and the TextBlock and define an ItemContainerStyle that stretches the content of the ComboBoxItem :摆脱StackPanelTextBlock并定义一个ItemContainerStyle来拉伸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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM