如何覆盖扩展到另一个控件wpf

时间:2014-03-03 11:50:11

标签: c# wpf xaml expander

我有一个如下代码片段。我想单击测试按钮以显示扩展器,并让扩展器覆盖同一行中的TextBlock和ComboBox。我已经尝试过某人的解决方案来设置ZIndex,但是不起作用。有人可以帮忙吗?

 <Window.Resources>
    <Storyboard x:Key="show" TargetProperty="Height">
        <DoubleAnimation Storyboard.TargetName="TestRec"  Duration="0:0:0.3" From="0" To="300"/>
    </Storyboard>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Content="showRectangle" Click="Button_Click"/>
    <TextBlock Text="Test TextBlock" Grid.Row="1"/>
    <ComboBox Grid.Column="1" Grid.Row="1" SelectedIndex="0">
        <ComboBoxItem Content="A"/>
        <ComboBoxItem Content="B"/>
        <ComboBoxItem Content="C"/>
        <ComboBoxItem Content="D"/>
    </ComboBox>
    <Grid Grid.Row="1" Grid.ColumnSpan="2" x:Name="TestRec" Height="0">
        <Expander Header="abc" IsExpanded="True">
            <Expander.Content>
                <ListView ItemsSource="{Binding List}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Expander.Content>
        </Expander>
    </Grid>
</Grid>


 private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.BeginStoryboard(FindResource("show") as Storyboard);
    }

3 个答案:

答案 0 :(得分:1)

将扩展器的背景设置为白色。

<Expander Header="abc" IsExpanded="True" Background="White">

答案 1 :(得分:0)

该解决方案可靠,我建议您将其隐藏在Storyboard中。为控件名称设置,在本例中使用TestTextBlockTestComboBox

<Window x:Class="MyProject.MainWindow"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"

...

<Storyboard x:Key="show">
    <DoubleAnimation Storyboard.TargetName="TestRec" 
                     Storyboard.TargetProperty="Height"
                     Duration="0:0:0.3" 
                     From="0" 
                     To="300" />

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TestTextBlock"
                                   Storyboard.TargetProperty="Opacity">

        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <sys:Double>0.0</sys:Double>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TestComboBox"
                                   Storyboard.TargetProperty="Opacity">

        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <sys:Double>0.0</sys:Double>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

在这种情况下,如果您使用DoubleAnimation属性,也可以使用Opacity。根据另一个Storyboard,您会通过将Opacity设置为1.0来显示它们。

答案 2 :(得分:0)

除@Bizz之外,如果pc不处于高对比度模式,您可以为Expander设置样式, 将背景设置为白色。

<Style.Triggers>
    <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="False">
        <Setter Property="Background" Value="White" />
    </DataTrigger>
    <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="True">
          <Setter Property="Background" Value="{Binding Source={x:Static SystemColors.ActiveBorderBrush}}"/>
    </DataTrigger>
</Style.Triggers>