仅返回选中框

时间:2015-07-16 15:38:43

标签: c# wpf xaml checkbox checkboxlist

编辑:它现在有效!感谢Alberto的详细解释,我将IsChecked绑定到一个变量,并按照他写的方式更改了button_1_click

我只想在XAML中以这种方式定义的复选框列表中仅返回复选框的内容:

<Grid>
    <StackPanel Width="250" Height="80"></StackPanel>
    <TextBox Name="ZoneText" Width="160" Height="20" Margin="12,215,330,76" Background="Bisque" />
    <TextBox Name="ZoneValue" Width="160" Height="20" Margin="12,239,330,52" Background="Bisque" />
    <ListBox Name="listBoxZone" ItemsSource="{Binding TheList}" Background="Azure" Margin="12,12,12,115">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="CheckBoxZone" Content="{Binding TheText}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button Content="Validate" Height="44" HorizontalAlignment="Left" Margin="362,215,0,0" Name="button1" VerticalAlignment="Top" Width="123" Click="button1_Click" />
</Grid>

我希望能够将选中复选框的所有内容存储在列表中(当我按下按钮时),但我似乎无法做到,这是我在C#中尝试过的:

public MainWindow()
{
    InitializeComponent();
    CreateCheckBoxList();
}

public class BoolStringClass
{
    public string TheText { get; set; }
    public int TheValue { get; set; }
}

public void CreateCheckBoxList()
{
    TheList = new ObservableCollection<BoolStringClass>();

    foreach (var line in File.ReadLines(path))
    {
        if (regex1.Match(line).Success)
        TheList.Add(new BoolStringClass { TheText = "" + line, TheValue = false });
        else
        TheList.Add(new BoolStringClass { TheText = "" + line, TheValue = true });

        this.DataContext = this;
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    foreach (CheckBox item in listBoxZone.Items)
    {
        List<string> selectedFOP = new List<String>();
        if (item.IsChecked == true)
        {
            selectedFOP.Add(item.Content.ToString());
        }
    }

}

但是我得到一个错误,就是不可能将类型为'BoolStringClass'的对象转换为'System.Windows.Controls.CheckBox'。我想我必须使用listBoxZone.ItemTemplate,但是如何访问其中的复选框? 谢谢

1 个答案:

答案 0 :(得分:1)

您必须将TheValue类型更改为布尔值

public class BoolStringClass
{
    public string TheText { get; set; }
    public bool? TheValue { get; set; }
}

并在复选框上绑定两个IsChecked属性(您不需要处理已检查的已更改事件)。

<CheckBox Name="CheckBoxZone" Content="{Binding TheText}" IsChecked="{Binding TheValue, Mode=TwoWay}" Margin="0,5,0,0" />

如果列表在初始化时可以有不同的值,则需要在BoolStringClass中实现INotifyPropertyChanged,以便复选框反映初始值(取决于您的设计,这可能不是必需的):

public class BoolStringClass : INotifyPropertyChanged
{
    private bool? _thevalue;
    public event PropertyChangedEventHandler PropertyChanged;

    public string TheText { get; set; }

    public bool? TheValue
    {
        get
        {
             return _thevalue;
        }
        set
        {
             _thevalue = value;

             OnPropertyChanged("TheValue")
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

同样在CreateCheckBoxList()中创建列表时,使用ObservableCollection(如果复选框的数量不会动态更改,但仅在此处初始化时,您可以使用正常的BoolStringClass列表。)

// use global variable
ObservableCollection<BoolStringClass> TheList;

public void CreateCheckBoxList()
{
    //some code that creates a list
    TheList = new ObservableCollection<BoolStringClass>() { 
        new BoolStringClass() {TheText = "Check1", TheValue = false},
        new BoolStringClass() {TheText = "Check2", TheValue = true} 
    }
}

然后你可以迭代列表(复制的列表创建也在循环之外!)

private void button1_Click(object sender, RoutedEventArgs e)
{
    List<string> selectedFOP = new List<String>();

    foreach (BoolStringClass item in TheList)
    {
        if (item.TheValue == true) selectedFOP.Add(item.ToString());
    }
}

根据您的程序设计方式,您可能不需要复制此副本。如果你真的这样做,你可以使用LINQ(使用System.LINQ添加)在一行中完成:

private void button1_Click(object sender, RoutedEventArgs e)
{
    var selectedFOP = TheList.Where(b => b.TheValue == true).ToList();
}

我手动完成,因此可能会出现轻微的语法错误。