簡體   English   中英

在C#中單擊SelectAll時的CheckAll復選框

[英]CheckAll checkboxes when SelectAll is clicked in c#

我有一個動態列表框,單擊selectAll按鈕時如何選擇列表框中的所有復選框。

Xaml:

StackPanel Width="400" Orientation="Horizontal" Grid.Row="0">
                    <!--<CheckBox IsThreeState="True" x:Name="cbAllFeatures" Width="111" Content="Select all" Height="78"/>-->
                    <AppBarButton x:Name="Btn_SelectAll" Margin="20,0,0,0" Label="Select All" Icon="SelectAll" Click="Btn_SelectAll_Click" FontWeight="Bold" />
                    <AppBarButton x:Name="Btn_Delete" Margin="100,0,0,0" Label="Delete All" Icon="Delete" Click="DeleteAll_Click" FontWeight="Bold" />
                </StackPanel>

<ListBox  x:Name="listBoxobj" ItemsSource="{Binding}" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Width="330" Height="100" >
                                <Border Margin="5" BorderBrush="White" BorderThickness="1">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="50" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="100" />
                                        </Grid.ColumnDefinitions>
                                        <CheckBox Name="Option1CheckBox" Grid.Row="0" Grid.Column="0"  IsChecked="{Binding Checked,Mode=TwoWay}"  VerticalAlignment="Center" Margin="5,0,0,0" />
                                        <TextBlock x:Name="NameTxt" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/>
                                        <TextBlock x:Name="Age" Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" />
                                        <Button Grid.Row="0" Grid.Column="2" x:Name="deleteTaskButton" BorderThickness="0" Margin="0"  Click="deleteTaskButton_Click" Height="18" IsEnabled="False">
                                            <Image Source="/Assets/delete.png"/>
                                        </Button>
                                    </Grid>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

我試圖用這種方式來做,但是似乎沒有用。

   class SelectAll : INotifyPropertyChanged
    {
        public bool _Checked = false;

        public bool Checked
        {
            get
            {
                return _Checked;
            }
            set
            {
                if (value != _Checked)
                {
                    _Checked = value;
                    NotifyPropertyChanged("Checked");
                }
            }
        }

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

單擊select_all時的代碼。

 public void Btn_SelectAll_Click(object sender, RoutedEventArgs e)
    {
      obj_check._Checked = true;
    }

我的想法是使用for循環。 它必須檢查每個項目。 但是我不知道如何檢查,因為我無法選擇Checkbox。感謝您的幫助。

嘗試這個

首先創建此功能:

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

創建一個新的布爾值:

bool Cheacked = false;

然后在您的按鈕上添加以下內容:

        if (!Cheacked)
        {
            IEnumerable<CheckBox> _CheackBox = FindVisualChildren<CheckBox>(this);
            foreach (CheckBox _CB in _CheackBox)
            {
                _CB.IsChecked = true;
            }
        }
        else
        {
            IEnumerable<CheckBox> _CheackBox = FindVisualChildren<CheckBox>(this);
            foreach (CheckBox _CB in _CheackBox)
            {
                _CB.IsChecked = false;
            }
        }

它將獲取窗口中的所有對象,然后將其標記為已選中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM