WPF GroupBox控件模板

时间:2016-04-18 12:26:58

标签: c# wpf

我有一个代码,其中我有groupbox,并且在包装面板中有复选框,它实际上包装打开了groupbox,这是一个要求,所以工作正常。 但是边框内的复选框也会取消选中我不想要的组合框。 您能否建议我如何创建一个功能,其中边框内的复选框不包装我的组框。

    <StackPanel.Resources>
        <Style TargetType="GroupBox" x:Key="groupBoxStyle">
            <Style.Triggers>
                <EventTrigger RoutedEvent="CheckBox.Unchecked">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Height" Duration="0:0:.2"  To="30" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="CheckBox.Checked">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Height" Duration="0:0:.2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <GroupBox Margin="8,0" Height="150" BorderBrush="Transparent" Style="{StaticResource groupBoxStyle}">
        <GroupBox.Header>
            <WrapPanel >
                <CheckBox IsChecked="True" VerticalAlignment="Center" />
                <Label Content="Volatility" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" />
            </WrapPanel>
        </GroupBox.Header>
        <Border BorderBrush="Black" BorderThickness="1">
            <UniformGrid Columns="2">
                <Label Content="Spots"></Label>
                <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
                <Label Content="Hist. references" />
                <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
                <Label Content="Tenors" />
                <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
                <Label Content="(5) Is there Value Plan? Yes/No" Margin="0,0,-42,0"  />
                <CheckBox Margin="0,5,0,0" Name="chkISvalue" HorizontalAlignment="Right" Width="84" Style="{x:Null}"  Checked="chkISvalue_Checked" Unchecked="chkISvalue_Unchecked" />
            </UniformGrid>
        </Border>
    </GroupBox>
</StackPanel>

1 个答案:

答案 0 :(得分:0)

您可以在EventTrigger中设置SourceName,因此它仅适用于正确的CheckBox。您还需要将EventTrigger从样式移动到GroupBox的触发器本身,以便您可以使用CheckBox名称。 完整的例子:

<StackPanel>    
<GroupBox Margin="8,0" Height="150" BorderBrush="Transparent" Style="{StaticResource groupBoxStyle}">
  <GroupBox.Triggers>
    <EventTrigger RoutedEvent="CheckBox.Unchecked" SourceName="WrappingCheckBox">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation Storyboard.TargetProperty="Height" Duration="0:0:.2"  To="30" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
    <EventTrigger RoutedEvent="CheckBox.Checked">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation Storyboard.TargetProperty="Height" Duration="0:0:.2" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </GroupBox.Triggers>
  <GroupBox.Header>
    <WrapPanel >
      <CheckBox Name="WrappingCheckBox" IsChecked="True" VerticalAlignment="Center" />
      <Label Content="Volatility" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" />
    </WrapPanel>
  </GroupBox.Header>
  <Border BorderBrush="Black" BorderThickness="1">
    <UniformGrid Columns="2">
      <Label Content="Spots"></Label>
      <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
      <Label Content="Hist. references" />
      <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
      <Label Content="Tenors" />
      <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
      <Label Content="(5) Is there Value Plan? Yes/No" Margin="0,0,-42,0"  />
      <CheckBox Margin="0,5,0,0" Name="chkISvalue" HorizontalAlignment="Right" Width="84" Style="{x:Null}" />
    </UniformGrid>
  </Border>
</GroupBox>