清除CheckBoxList的值?

时间:2011-08-11 18:13:33

标签: c# wpf checkbox checkboxlist

我有一个带复选框的listBox,当事件结束时,我需要取消选中每个复选框。

这是我的xaml,但在代码中如何清除这些值?感谢

<ListBox Name="_employeeListBox" ItemsSource="{Binding employeeList}" Margin="409,27,41,301">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <CheckBox Grid.Column="0" Name="CheckBoxZone" Content="{Binding OperatorNum}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked"/>
                <TextBlock Grid.Column="1"></TextBlock>
                <TextBlock Grid.Column="2" Text="{Binding Name}"></TextBlock>
                <TextBlock Grid.Column="3"></TextBlock>
                <TextBlock Grid.Column="4" Text="{Binding LastName}"></TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

  private void _searchProjectsbyEmployeebutton_Click_1(object sender, RoutedEventArgs e)
    {
        List<EmployeebyProject> employeebyProjectList = new List<EmployeebyProject>();
        if (EmployeeCollectionToInsert.Count != 0)
        {
            foreach (var employee in EmployeeCollectionToInsert)
            {
                foreach(var employeebyProject in employee.EmployeebyProject)
                {
                    employeebyProjectList.Add(employeebyProject);
                }
            }
            LoadEmployeebyProject(employeebyProjectList);  
            //HERE I NEED UNCHECKED the ListBoxChecked.             
        }
        else { MessageBox.Show("Please select an employee to search his project."); }
    }

2 个答案:

答案 0 :(得分:2)

您在ItemsSource中绑定ListBoxListBox正在使用VirtualizingStackPanel作为其ItemsPanel,因此所有ListBoxItem个容器可能会生成也可能不会生成任何给定的时间 因此,即使您搜索Visual Tree等以取消选中所有CheckBox,您也无法确定是否已取消选中它们。

我建议您将IsChecked绑定到与您定义的OperatorNum相同的类中的新属性(根据您的问题的外观,可能是Employee或类似的)。这样,取消选中CheckBox所需要做的就是在源类中将IsChecked设置为False
另外,请确保您实施INotifyPropertyChanged

示例Xaml

<CheckBox Grid.Column="0"
          Name="CheckBoxZone"
          Content="{Binding OperatorNum}"
          Tag="{Binding TheValue}"
          IsChecked="{Binding IsChecked}"/>

员工

public class Employee : INotifyPropertyChanged
{
    private bool m_isChecked;
    public bool IsChecked
    {
        get { return m_isChecked; }
        set
        {
            m_isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }
    // Etc..

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

答案 1 :(得分:1)

This帖子解释了如何获取基于Template ListBox的项目。您可以使用类似的技术查找CheckBox,一旦获得CheckBox,检查或取消选中就很简单