清除 CheckBoxList 的值?

发布于 2024-11-29 03:58:30 字数 1962 浏览 1 评论 0原文

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

这是我的 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."); }
    }

I have one listBox with check box, when an event end, I need uncheck each check box.

This is my xaml, but in code how can I clear this values? Thanks

<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."); }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-12-06 03:58:30

您正在 ListBox 中绑定 ItemsSource,并且 ListBox 使用 VirtualizingStackPanel 作为其 ItemsPanel,因此所有 ListBoxItem 容器可能会也可能不会在任何给定时间生成。
因此,即使您搜索视觉树等以取消选中所有复选框,您也无法确定是否已全部取消选中。

我建议您将 IsChecked 绑定到与您定义的 OperatorNum 相同的类中的新属性(从您的问题来看,它可能是 Employee 或类似的属性)。这样,您只需在源类中将 IsChecked 设置为 False 即可取消选中复选框。
另请确保您实现了 INotifyPropertyChanged

Example Xaml

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

Employee

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));
        }
    }
}

You are binding ItemsSource in the ListBox and the ListBox is using VirtualizingStackPanel as its ItemsPanel so all ListBoxItem containers may or may not be generated at any given time.
So even if you search the Visual Tree etc. to get all the CheckBoxes to uncheck, you can't be sure that you've unchecked them all.

I suggest you bind IsChecked to a new property in the same class as you've defined OperatorNum (which, by the looks of your question, probably is Employee or similar). This way, all you need to do to uncheck the CheckBoxes is to set IsChecked to False in your source class.
Also make sure you implement INotifyPropertyChanged

Example Xaml

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

Employee

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));
        }
    }
}
没︽人懂的悲伤 2024-12-06 03:58:30

这篇文章介绍了如何获取模板的项目 基于ListBox。您可以使用类似的技术来查找 CheckBox,一旦获得 CheckBox,选中或取消选中就很简单

This post explains how you can get the items of a Template based ListBox. You can use similar technique to find the CheckBox and once you have have got the CheckBox, checking or unchecking is simple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文