【问题标题】:Get the IsChecked property value from a list of checkbox in a combobox从组合框中的复选框列表中获取 IsChecked 属性值
【发布时间】:2017-12-02 00:58:49
【问题描述】:

我正在编写一个程序,其中有一个由一系列复选框组成的组合框。 用户可以选择任意数量的复选框。之后,当他单击一个按钮时,我想检查所有复选框以查看哪些已选中,哪些未选中。

但我不知道该怎么做。我尝试了很多方法,但没有一个能返回我的财产价值。事实上,我无法从代码中获取绑定属性。代码如下:

在 WPF 中:

<ComboBox x:Name="month_comboBox" Margin="349,107,0,0" Height="22" VerticalAlignment="Top" Visibility="Hidden">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Width="20" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>
                <TextBlock Text="{Binding Path=month_Name}" Width="100" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在.cs文件中,这是加载combobx的代码:

在主类中有这个方法:

private void LoadMonths()
    {
        MonthList ML = new MonthList();
        month_comboBox.ItemsSource = ML.Months;
        month_comboBox.SelectedIndex = 0;
    }

元素也有这两个类:

public class MonthList
    {
        ObservableCollection<Month> _Months = new ObservableCollection<Month>();

        public MonthList()
        {
            _Months.Add(new Month { month_Name = "every month" });
            _Months.Add(new Month { month_Name = "january" });
            _Months.Add(new Month { month_Name = "february" });
            _Months.Add(new Month { month_Name = "march" });
            _Months.Add(new Month { month_Name = "april" });
            _Months.Add(new Month { month_Name = "may" });
            _Months.Add(new Month { month_Name = "june" });
            _Months.Add(new Month { month_Name = "july" });
            _Months.Add(new Month { month_Name = "august" });
            _Months.Add(new Month { month_Name = "september" });
            _Months.Add(new Month { month_Name = "october" });
            _Months.Add(new Month { month_Name = "november" });
            _Months.Add(new Month { month_Name = "december" }); 
        }

        public ObservableCollection<Month> Months { get { return _Months; } }

    }

    public class Month
    {
        private string monthName;

        public bool IsSelected {
            get { return IsSelected; }
            set; 
        }

        public string month_Name
        {
            get { return monthName; }
            set { monthName = value; }
        }

    }

现在,我想要的是一种方法来知道,对于组合框中的每个复选框,该复选框是否被选中。

我有这个,但我不知道里面放了什么指令来获取属性值:

for (int i = 0; i < month_comboBox.Items.Count; i++) { 
    //Instruction missing here
}

我知道我必须使用与此类似的东西

month_comboBox.Items[i]

但我不知道我必须挂钩它以获得属性值。

谢谢大家

【问题讨论】:

  • 您是否尝试将Items[i] 转换为CheckBox 并连接到Checked property?
  • 是的,但我的问题是我无法从month_comboBox.Items[i] 获得对其中checkBox 的引用。当我使用month_comboBox.Items[i]时,我找不到任何东西,从这里引导我到checkBox的IsChecked属性。
  • 看看MonthList,你已经进入了 MVVM 的一半。在 MVVM 的精彩世界中,您无需处理 CheckBoxes 或 ComboBoxes,而是处理正确的事情,例如个月。当用户单击按钮并执行ICommand 时,您只需检查Month 集合即可找到IsSelected 的月份。
  • 那么请随意标记我的答案:)

标签: c# wpf checkbox combobox


【解决方案1】:

这很简单 - 像这样投射:

public void GetValues()
{
    foreach(var item in month_comboBox.Items)
    {
        Month temp = item as Month;
        bool isSelected = temp.IsSelected;
        //continue here :)
    }
}

您也可以使用模式匹配。

        if (item is Month month)
        {
            var isSelected = month.IsSelected;
            //continue here :)
        }

或者可能是这样的:

    public IEnumerable<Month> GetValues()
    {
        foreach(var item in month_comboBox.Items)
        {
            if(item is Month month) return month;
        }
    }

另外请查看MVVMhere

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多