如何使WPF Expander向上扩展,同时保持标头固定

时间:2010-12-08 21:58:00

标签: wpf canvas overlay fixed expander

我正在尝试创建一个Expander,其中标题保持固定,内容显示在标题上方,覆盖上面的任何其他控件。设置ExpandDirection="Up"并将Expander放入Canvas中可实现一半 - 内容相对于标题向上扩展,并覆盖其他控件(尽管如下),但标题向下移动。

有没有办法做到这一点,但将标题保持在固定位置,以便内容最终覆盖上面的控件?

这是我到目前为止所做的:

<Window x:Class="Sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="900" Width="1100">
  <StackPanel>

    <StackPanel Margin="20,0,0,0">
      <RadioButton Content="Choice One"/>
      <RadioButton Content="Choice Two"/>
      <RadioButton Content="Choice Three"/>
      <RadioButton Content="Choice Four"/>
      <RadioButton Content="Choice Five"/>
      <RadioButton Content="Choice Six"/>
    </StackPanel>

    <Canvas MinHeight="25" Panel.ZIndex="99">
      <Expander Header="This must stay fixed" ExpandDirection="Up" Width="175">
        <Grid Background="Cornsilk">
          <Grid.BitmapEffect>
            <DropShadowBitmapEffect />
          </Grid.BitmapEffect>

          <TextBlock TextWrapping="Wrap" Margin="5">
            This must expand upwards, not downwards.
            The header must remain exactly where it is.
            This TextBlock must appear above the header
            and overlay the top radio buttons instead.
          </TextBlock>
        </Grid>
      </Expander>
    </Canvas>

    <StackPanel Margin="20,0,0,0">
      <RadioButton Content="Choice One"/>
      <RadioButton Content="Choice Two"/>
      <RadioButton Content="Choice Three"/>
      <RadioButton Content="Choice Four"/>
      <RadioButton Content="Choice Five"/>
      <RadioButton Content="Choice Six"/>
    </StackPanel>

  </StackPanel>
</Window>

2 个答案:

答案 0 :(得分:3)

您可以使用ToggleButton和Popup代替Expander:

<Canvas MinHeight="25" Panel.ZIndex="99">
  <ToggleButton x:Name="toggle" Content="This must stay fixed" Width="175" />
  <Popup Placement="Top" PlacementTarget="{Binding ElementName=toggle}"
         IsOpen="{Binding ElementName=toggle, Path=IsChecked}">
      <Grid Background="Cornsilk" Width="175">
          <Grid.BitmapEffect>
              <DropShadowBitmapEffect />
          </Grid.BitmapEffect>

          <TextBlock TextWrapping="Wrap" Margin="5">
            This must expand upwards, not downwards.
            The header must remain exactly where it is.
            This TextBlock must appear above the header
            and overlay the top radio buttons instead.
          </TextBlock>
      </Grid>  
  </Popup>
</Canvas>

答案 1 :(得分:0)

另一种方法是使用CheckBox控件而不是ToggleButton。使用例如,可以很容易地隐藏盒子。 StackPanel具有负边际值。此外,您不需要关心边界等。在某些情况下,它更容易。