2012-01-17 91 views
0

我有一個帶有作爲父組件的StackPanel(名稱爲MainStackPanel)的WPF窗體。它有各種各樣的groupboxes作爲它的孩子。每個組框有兩個複選框(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> 
+0

到目前爲止你做了什麼/試過了嗎?向我們展示一些代碼可以幫助很多。 – 2012-01-17 21:32:31

+0

這裏是堆棧面板代碼和組框代碼。 (組框有一個網格,複選框在網格內) – user1154892 2012-01-17 21:36:54

+0

你有模型支持你的用戶界面嗎? – 2012-01-17 21:48:22

回答

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; 
    } 
} 
+0

謝謝大家的幫助。讓我在我的代碼中嘗試 – user1154892 2012-01-18 13:49:38