在 ComboBoxItem(组合框列表)中切换复选框,而不选择 WPF - C# - XAML 中的项目

问题描述 投票:0回答:1

我在 WPF 应用程序中有一个

ComboBox
,其中包含多个
ComboBoxItems
,每个都托管一个
CheckBox
。我希望用户能够在不实际选择 ComboBoxItem 的情况下切换 CheckBox。目前,如果我单击复选框,它会正确检查该复选框,但如果我单击该项目(而不是复选框),它会选择该项目,而我只是想使用此列表来选择复选框,并且不想已选择任何项目。

演示 - 单击复选框时:

演示 - 如果我点击该项目:

如您所见,当我单击该项目时,在复选框上,它会检查该项目,但如果我单击该项目,它就会选择并显示该项目。 我只关心检查项目而不想:

  1. 关闭下拉菜单时显示任何项目
  2. 能够选择任何项目。

XAML 代码:

<ComboBox Margin="40,10,0,2" Name="ComboBox1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="250" Height="30" Loaded="ComboBox1_Loaded">
                                        <!-- The items will be added programmatically -->
                                    </ComboBox>

C# 代码背后:

private void ComboBox1_Loaded(object sender, RoutedEventArgs e)
        {
            // List of items to be added to the ComboBox
            string[] items = new string[] {
                "Example1", "Example2", "Example3", "Example14", "Example5", 
            };

            foreach (string item in items)
            {
                // Create a new CheckBox
                System.Windows.Controls.CheckBox checkBox = new System.Windows.Controls.CheckBox
                {
                    Content = item,
                    Name = "chk" + item.Replace(" ", "")
                };

                // Create a new ComboBoxItem and add the CheckBox to it
                ComboBoxItem comboBoxItem = new ComboBoxItem();
                comboBoxItem.Content = checkBox;

                // Add the ComboBoxItem to the ComboBox
                ComboBox1.Items.Add(comboBoxItem);
            }
        }

        private void ComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            // StringBuilder to hold all checked items
            var checkedItems = new StringBuilder();

            foreach (ComboBoxItem comboBoxItem in ComboBox1.Items)
            {
                if (comboBoxItem.Content is System.Windows.Controls.CheckBox checkBox && checkBox.IsChecked == true)
                {
                    // Add the checked item to the StringBuilder
                    checkedItems.AppendLine(checkBox.Content.ToString());
                }
            }

            // Check if we have any checked items and write them to debug output
            if (checkedItems.Length > 0)
            {
                Debug.WriteLine("Checked Items:");
                Debug.WriteLine(checkedItems.ToString());
            }
        }

我希望这是你能帮助我的事情。

最好的,

c# wpf xaml combobox
1个回答
0
投票

如果不需要ComboBox的功能,使用Menu等其他控件就足够了。这是使用 CommunityToolkit.Mvvm 的示例。

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;

public partial class MainWindowViewModel : ObservableObject
{
    public ObservableCollection<ItemViewModel> Items { get; } = new();

    public MainWindowViewModel()
    {
        // Populate Items.
    }
}

public partial class ItemViewModel : ObservableObject
{
    [ObservableProperty]
    string _name;

    [ObservableProperty]
    bool _isSelected;

    public ItemViewModel(string name) => this.Name = name;
}
<Menu>
    <MenuItem Header="Transportation"
              ItemsSource="{Binding Items}">
        <MenuItem.ItemContainerStyle>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type MenuItem}">
                            <CheckBox Content="{Binding Name, Mode=OneTime}"
                                      IsChecked="{Binding IsSelected}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
</Menu>

您可以通过迭代ObservableCollection来检查每个项目是否被检查。

© www.soinside.com 2019 - 2024. All rights reserved.