【问题标题】:Checkbox from ComboBoxItem is binded to Boolean in Class. How to get every Boolean from every single Item?ComboBoxItem 中的复选框绑定到 Class 中的布尔值。如何从每个项目中获取每个布尔值?
【发布时间】:2021-02-24 23:24:05
【问题描述】:

很抱歉我的英语不好,请记住我刚刚开始使用 WPF,所以它甚至可能不是最漂亮的方法。

我试图删除选中复选框的每个项目,但我有问题指向复选框。所以我试图指向一个类中的布尔值。但也存在问题。然后我投降了,来到了这里。

感谢任何帮助。

我的 Button 应该从列表中删除某个项目,选中复选框,如下所示。

private void DeleteComboBoxItem_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < ComboBoxName.Items.Count; i++)
            {
                if (ComboBoxName.Item.Contains((CheckBox).Equals(True))) <--- Have Problems here.
                {
                    List.RemoveAt(i);
                }
            }
        }

列表中只有猫的名字 - 我有一个公式可以添加猫的名字。

我有一个带有以下模板的组合框:

<ComboBox x:Name="ComboBoxName">
     <ComboBox.ItemTemplate>
           <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="Auto"/>
                         <ColumnDefinition Width="*"/>
                     </Grid.ColumnDefinitions>
                     <CheckBox Grid.Column="0" IsChecked="{Binding b_CheckBox}" VerticalContentAlignment="Center"/>
                     <TextBlock Grid.Column="1" Text="{Binding tb_Name}" />
                </Grid>
           </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

还有一个看起来像这样的类。

public class Cat 
{
public bool b_Checkbox {get; set;}
public string tb_Name {get; set;}
}

希望你能帮助我。

【问题讨论】:

    标签: c# wpf xaml checkbox combobox


    【解决方案1】:

    您希望复选框触发移除猫,对吗?不知道这是不是最有效的方法,但它确实有效

    1. 每次 Cat 类中的 bool 属性更改时添加一个事件:

       public class Cat 
       {
      
           public event Action<bool> CheckboxChanged;
      
           private bool imChecked;
           public bool ImChecked
           {
               get
               {
                   return imChecked;
               }
               set
               {
                   imChecked = value;
                   CheckboxChanged?.Invoke(value);
               }
           }
      
      
           public string tb_Name { get; set; }            
       }     
      
    2. 在 ViewModel 中设置每次触发此事件时要执行的操作

     //In the constructor of your ViewModel, iterate through each cat and subscribe to its event. Then, give the cats to the comboBox
     public YourViewModel()
            {
                InitializeComponent();
    
    
                //Getting the cats
                ObservableCollection<Cat> Cats = new ObservableCollection<Cat>
                {
                    new Cat () {tb_Name = "1st cat", ImChecked = false},
                    new Cat () {tb_Name = "2nd cat", ImChecked = false},
                    new Cat () {tb_Name = "3rd cat", ImChecked = false},
                    new Cat () {tb_Name = "4th cat", ImChecked = false},
                };
    
               //Subscribing to each cat's event
                foreach (Cat cat in Cats)
                {
                    cat.CheckboxChanged += CatChanged;
                }
    
                //Give the comboBox the cats
                YourComboBox.ItemsSource = Cats;           
            }
    
    
     //This is what's gonna happen each time the ImChecked propierty in a cat changes
     private void CatChanged(bool catIsChecked)
            {
                //Create a empty collection to add the cats that are not checked
                ObservableCollection<Cat> catsToKeep = new ObservableCollection<Cat>();
    
                //Grab the ones not checked
                for (int i = 0; i < YourComboBox.Items.Count; i++)
                {
                    Cat currentCat = YourComboBox.Items[i] as Cat;
    
                    if (currentCat.ImChecked == false)
                    {
                        catsToKeep.Add(currentCat);
                    }
                }
                
                //Give the unchecked cats to the comboBox again
                YourComboBox.ItemsSource = catsToKeep;
            }
    
    

    Xaml:

    <ComboBox x:Name="YourComboBox"
                          Height="30"
                          Width="150">
                    
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
    
                            <CheckBox IsChecked="{Binding ImChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                                <TextBlock Text="{Binding tb_Name}"/>
                            </CheckBox>
    
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
    
                </ComboBox>
    
    
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多