【问题标题】:Clear values of a CheckBoxList?清除 CheckBoxList 的值?
【发布时间】:2011-10-25 05:45:08
【问题描述】:

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

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

【问题讨论】:

  • 我建议您的应用程序需要在 View 和 ViewModel 之间进行更多分离。

标签: c# wpf checkbox checkboxlist


【解决方案1】:

您在ListBox 中绑定ItemsSource,而ListBox 使用VirtualizingStackPanel 作为其ItemsPanel,因此所有ListBoxItem 容器可能会或可能不会在任何给定时间生成。
因此,即使您搜索 Visual Tree 等以取消选中所有 CheckBox,您也不能确定是否已全部取消选中。

我建议您将IsChecked 绑定到与您定义OperatorNum 相同的类中的新属性(从您的问题来看,可能是Employee 或类似的)。这样,取消选中复选框所需要做的就是在源类中将 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));
        }
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 问题是我在 wpf 中工作,而 FindVisualChild 在这里不起作用,也许你有其他参考来做这个......
    • 我发布的链接是 WPF 示例,FindViusalChild 绝对有效。这是同一篇文章中写的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多