【问题标题】:Checkbox controls in a groupbox which is a child of a parent stackpanel组框中的复选框控件,它是父堆栈面板的子项
【发布时间】:2012-01-17 21:21:01
【问题描述】:

我有一个 WPF 表单,其父级为 stackpanel(名称为 MainStackPanel)。它有各种分组框作为它的孩子。每个 groupbox 有两个复选框(checkbox1 和 checkbox2)。

现在我想在 Mainstack 面板中添加一个检查所有按钮,单击该按钮将自动检查每个组中的所有 checkbox1。

我是 WPF 新手,正在尝试了解如何实现这一目标

<EDIT>

<StackPanel x:Name="MainStackPanel" Orientation="Vertical"> 

   <Grid DataContext="{Binding}"> 

      <Button Content="UnCheck All" Height="23" Name="uncheckall" 
              Width="75" Margin="434,0,492,0" /> 

      <Button Content="Check All" Height="23" Name="checkall" Width="75"
              Margin="175,0,751,0" Click="checkall_Click" />

   </Grid>

   <GroupBox>
      <Grid>
         <Grid.RowDefinitions>
           <RowDefinition Height="30"/>
           <RowDefinition/>
         </Grid.RowDefinitions>

         <CheckBox x:Name="checkbox1" 
                   Style="{StaticResource styleCheckBoxLeftSideText}" 
                   IsChecked="{Binding Path=Disabled, 
                               Converter={StaticResource BooleanConverter},
                               ConverterParameter='false,true'}"
                   VerticalAlignment="Center" 
                   HorizontalAlignment="Left" 
                   Content="Task Enabled:" 
                   Margin="9,0,0,0"/>
      </Grid>
   </GroupBox> 
</StackPanel>

</EDIT>

【问题讨论】:

  • 到目前为止你做了/尝试了什么?向我们展示一些代码会很有帮助。
  • 这里是stackpanel代码和groupbox代码。 (组框有一个网格,复选框在网格内)
  • 你有模型支持你的 UI 吗?

标签: wpf children


【解决方案1】:

这样的事情应该可以工作:

但正如 jberger 建议的那样:看看 MVVM,它会让你的任务变得更容易......

XAML:

<GroupBox>
  <StackPanel>
    <CheckBox x:Name="checkbox1" Margin="5">CheckBox1</CheckBox>
    <CheckBox x:Name="checkbox2" Margin="5">CheckBox2</CheckBox>
  </StackPanel>
</GroupBox>

<GroupBox>
  <StackPanel>
    <CheckBox x:Name="checkbox3" Margin="5">CheckBox1</CheckBox>
    <CheckBox x:Name="checkbox4" Margin="5">CheckBox2</CheckBox>
  </StackPanel>
</GroupBox>

助手类: (见How can I find WPF controls by name or type?

public static class VisualTreeExtensions
{
  public static IEnumerable<T> FindChildren<T>(this DependencyObject source)
    where T : DependencyObject
  {
    if (source != null)
    {
      IEnumerable<DependencyObject> childs = GetChildObjects(source);
      foreach (DependencyObject child in childs)
      {
        //analyze if children match the requested type
        if (child != null && child is T)
        {
          yield return (T)child;
        }

        //recurse tree
        foreach (T descendant in FindChildren<T>(child))
        {
          yield return descendant;
        }
      }
    }
  }

  public static IEnumerable<DependencyObject> GetChildObjects(
    this DependencyObject parent)
  {
    if (parent == null) yield break;

    if (parent is ContentElement || parent is FrameworkElement)
    {
      //use the logical tree for content / framework elements
      foreach (object obj in LogicalTreeHelper.GetChildren(parent))
      {
        var depObj = obj as DependencyObject;
        if (depObj != null) yield return (DependencyObject)obj;
      }
    }
    else
    {
      //use the visual tree per default
      int count = VisualTreeHelper.GetChildrenCount(parent);
      for (int i = 0; i < count; i++)
      {
        yield return VisualTreeHelper.GetChild(parent, i);
      }
    }
  }
}

代码隐藏:

private void checkall_Click(object sender, RoutedEventArgs e)
{
  SetCheckBoxCheckedStatus(true);
}

private void uncheckall_Click(object sender, RoutedEventArgs e)
{
  SetCheckBoxCheckedStatus(false);
}

private void SetCheckBoxCheckedStatus(bool isChecked)
{
  foreach (CheckBox check in MainStackPanel.FindChildren<CheckBox>())
  {
    check.IsChecked = isChecked;
  }
}

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2012-03-08
    • 2023-04-06
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多