如何堆叠基于控件的重叠按钮

时间:2017-02-08 17:20:26

标签: c# wpf

我正在尝试为基本对话框创建一个控件模板,它可以包含可以显示在内容之上的预定义按钮。问题是按钮没有与内容正确堆叠。

这是我目前得到的结果:

issue

我希望得到这样的东西:

final result

这是基本对话框资源的代码:

<ControlTemplate x:Key="BaseDialogTemplate" TargetType="{x:Type dialogs:BaseDialog}">
    <Grid Background="{TemplateBinding Background}">
        <ScrollViewer VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Auto">
            <Grid>
                <ContentPresenter x:Name="PART_ContentPresenter"
                                  Content="{TemplateBinding Content}" />
                <Grid HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch">
                    <StackPanel x:Name="PART_ButtonStackPanel"
                                Margin="10"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Orientation="Horizontal">
                        <Button x:Name="PART_OkButton"
                                MinWidth="80"
                                Margin="0 0 5 0"
                                Background="Yellow"
                                Content="Base Ok Button" />
                        <Button x:Name="PART_CancelButton"
                                MinWidth="80"
                                Margin="5 0 5 0"
                                Background="Yellow"
                                Content="Base Cancel Button" />
                    </StackPanel>
                </Grid>
            </Grid>
        </ScrollViewer>
    </Grid>
</ControlTemplate>

<Style TargetType="{x:Type dialogs:BaseDialog}">
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="White" />
    <Setter Property="Template" Value="{StaticResource BaseDialogTemplate}" />
</Style>

1 个答案:

答案 0 :(得分:0)

包含堆栈面板的ContentPresenter和Grid都是Grid.Row 0的一部分。尝试添加RowDefinitions来分隔内容,如下所示:

<ControlTemplate x:Key="BaseDialogTemplate" TargetType="{x:Type dialogs:BaseDialog}">
    <Grid Background="{TemplateBinding Background}">
        <ScrollViewer VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Auto">
            <Grid>
                <Grid.RowDefinitions>
                     <RowDefinition Height="*" />
                     <RowDefinition Height="Auto" />                         
                </Grid.RowDefinitions>
                <ContentPresenter x:Name="PART_ContentPresenter"
                                  Content="{TemplateBinding Content}" />
                <Grid Grid.Row="1" HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch">
                    <StackPanel x:Name="PART_ButtonStackPanel"
                                Margin="10"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Orientation="Horizontal">
                        <Button x:Name="PART_OkButton"
                                MinWidth="80"
                                Margin="0 0 5 0"
                                Background="Yellow"
                                Content="Base Ok Button" />
                        <Button x:Name="PART_CancelButton"
                                MinWidth="80"
                                Margin="5 0 5 0"
                                Background="Yellow"
                                Content="Base Cancel Button" />
                    </StackPanel>
                </Grid>
            </Grid>
        </ScrollViewer>
    </Grid>
</ControlTemplate>

<Style TargetType="{x:Type dialogs:BaseDialog}">
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="White" />
    <Setter Property="Template" Value="{StaticResource BaseDialogTemplate}" />
</Style>