C# 如何从基于控件堆叠重叠的按钮
我正在尝试为基本对话框创建一个控件模板,其中可以包含预定义的按钮,这些按钮可以显示在内容的顶部。问题是按钮与内容的堆叠不正确 这是我目前得到的结果: 我希望得到这样的东西:C# 如何从基于控件堆叠重叠的按钮,c#,wpf,C#,Wpf,我正在尝试为基本对话框创建一个控件模板,其中可以包含预定义的按钮,这些按钮可以显示在内容的顶部。问题是按钮与内容的堆叠不正确 这是我目前得到的结果: 我希望得到这样的东西: <ControlTemplate x:Key="BaseDialogTemplate" TargetType="{x:Type dialogs:BaseDialog}"> <Grid Background="{TemplateBinding Background}"> <
<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>
这是基本对话框资源的代码:
<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>
您的ContentPresenter和包含堆栈面板的网格都是网格的一部分。第0行。尝试添加行定义以分隔内容,如下所示:
<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>
感谢您的回复。我以前试过这样做,但没有达到我想要的效果。按钮不会出现在内容的顶部(不会覆盖内容),实际上,我认为这可能是我目前的最佳选择。我将把这个标记为答案。非常感谢。