Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅返回复选框的值_C#_Wpf_Xaml_Checkbox_Checkboxlist - Fatal编程技术网

C# 仅返回复选框的值

C# 仅返回复选框的值,c#,wpf,xaml,checkbox,checkboxlist,C#,Wpf,Xaml,Checkbox,Checkboxlist,编辑:现在可以了!多亏了阿尔贝托的详细解释,我选择了一个变量,并改变了按钮,点击了他写的方式 我只想返回在XAML中以这种方式定义的复选框列表中复选框的内容: <Grid> <StackPanel Width="250" Height="80"></StackPanel> <TextBox Name="ZoneText" Width="160" Height="20" Margin="12,215,330,76" Background="

编辑:现在可以了!多亏了阿尔贝托的详细解释,我选择了一个变量,并改变了按钮,点击了他写的方式

我只想返回在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主窗口()
{
初始化组件();
CreateCheckBoxList();
}
公共类BoolStringClass
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
public void CreateCheckBoxList()
{
列表=新的可观察集合();
foreach(File.ReadLines(路径)中的var行)
{
if(regex1.Match(line.Success)
Add(新BoolStringClass{TheText=”“+行,TheValue=false});
其他的
Add(新BoolStringClass{TheText=”“+行,TheValue=true});
this.DataContext=this;
}
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
foreach(listBoxZone.Items中的复选框项)
{
List SELECTED FOP=新列表();
如果(item.IsChecked==true)
{
选择fop.Add(item.Content.ToString());
}
}
}
但我得到一个错误,即不可能将类型为
'BoolStringClass'
的对象强制转换为
'System.Windows.Controls.CheckBox'
。我想我必须改用
listBoxZone.ItemTemplate
,但是我如何才能访问其中的复选框呢?
谢谢

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

public class BoolStringClass
{
    public string TheText { get; set; }
    public bool? TheValue { get; set; }
}
并双向绑定复选框上的IsChecked属性(您不需要处理checked changed事件)

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


我是手工做的,所以可能会有一些小的语法错误。

我需要一点时间来处理所有这些;)在将类型更改为bool并在XAML中双向绑定后,仍然会出现相同的错误。我将尝试其他更改,因为我在最初的帖子中添加了创建列表的方法。我想我不需要改变我的财产,是吗?您的解决方案让我有些困扰:
不应该与复选框是否被选中相关,也许我应该在
BoolStringClass
中添加第三个变量,以绑定
是否被选中
public class BoolStringClass
{
    public string TheText { get; set; }
    public bool? TheValue { get; set; }
}
<CheckBox Name="CheckBoxZone" Content="{Binding TheText}" IsChecked="{Binding TheValue, Mode=TwoWay}" Margin="0,5,0,0" />
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));
        }
    }
}
// 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());
    }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    var selectedFOP = TheList.Where(b => b.TheValue == true).ToList();
}