如何设置StackPanel中所有ToggleButtons的IsChecked属性为false

时间:2017-04-06 14:09:38

标签: c# wpf

如何在StackPanel中列出所有现有的ToggleButtons,并在代码隐藏中将其IsChecked属性设置为false

我开始时:

List<ToggleButton> togglebuttons = togglebtnListSp.Children.OfType<ToggleButton>().ToList();
        foreach(var item in togglebuttons)
        {
          item.IsChecked = false;  
        }

但现在我卡住了,不知道如何确认。

这是我的XAML:

<!-- Private Customer Rights -->
<StackPanel x:Name="togglebtnListSp">
            <GroupBox x:Name="customerRightsGroupBox" Header="Customer Rights" Margin="10,10,10.2,0" VerticalAlignment="Top" Height="108">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <StackPanel Margin="0,0,5,0">
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Add:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_add_customer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Create Assignment:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_add_assignment_customer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                    </StackPanel>
                    <StackPanel Grid.Column="1" Margin="5,0,0,0">
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Delete:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_delete_customer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Edit:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_edit_customer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                    </StackPanel>
                </Grid>
            </GroupBox>
            <!-- Firm Customer Rights -->
            <GroupBox x:Name="firmCustomerRightsGroupBox" Header="Firmcustomer rights"  Margin="10,10,10,0" VerticalAlignment="Top" Height="108">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <StackPanel Margin="0,0,5,0">
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Add:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_add_firmcustomer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                        <DockPanel LastChildFill="False">
                            <Label Content="Can create Assignment for firmcustomer" DockPanel.Dock="Left" FontSize="12" />
                            <ToggleButton x:Name="can_add_assignment_firmcustomer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                    </StackPanel>
                    <StackPanel Grid.Column="1" Margin="5,0,0,0">
                        <DockPanel LastChildFill="False">
                            <Label Content="Can Delete:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_delete_firmcustomer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                        <DockPanel LastChildFill="False">
                            <Label Content="Can edit:" DockPanel.Dock="Left" />
                            <ToggleButton x:Name="can_edit_firmcustomer" DockPanel.Dock="Right"></ToggleButton>
                        </DockPanel>
                    </StackPanel>
                </Grid>
            </GroupBox>
         </StackPanel>

我从数据库中读取false或true。但如果我点击&#34;添加&#34;我希望所有ToggleButtons都是IsChecked="false"

3 个答案:

答案 0 :(得分:1)

而不是将其添加到列表中,而只是创建一个linq命令 哪个

为您创建列表,并为您执行此操作

YourStackPanel.Children
              .OfType<ToggleButton>().ToList()
                                     .ForEach(toggleButton => toggleButton.IsChecked = false);

答案 1 :(得分:0)

试试这个。

for (int i = 0; i < togglebtnListSp.Children.Count; i++)
     if (_panel.Children[i] is ToggleButton)
              ((ToggleButton)_panel.Children[i]).IsChecked = false;

这里发生的是代码检查每个孩子是否是ToggleButton。如果是,则将集合中的UIElement对象强制转换为ToggleButton并将IsChecked设置为false。

答案 2 :(得分:0)

在code-behinde中,您可以使用方法按类型

查找所有元素
public static IEnumerable<T> SeachUiElementsByTypo<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 SeachUiElementsByTypo<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

像这样使用:

var toggleButtons= UiServices.SeachUiElementsByTypo<ToggleButton>(YourWindow);
foreach (var tb in toggleButtons)
    ((ToggleButton)tb).IsChecked = false