c# - 使用自动窗口大小设置按钮边距

标签 c# wpf

我创建了一个简单的自定义消息框,它会根据要显示的文本的长度自动缩放:

public partial class CustomMessageBox : Window
{
    public CustomMessageBox(string title, string text)
    {
        InitializeComponent();
        ResizeMode = ResizeMode.NoResize;
        label.Content = text;
        Title = title;
    }

    public static void Show(string title, string text)
    {
        CustomMessageBox box = new CustomMessageBox(title, text);
        box.SizeToContent = SizeToContent.WidthAndHeight;
        box.ShowDialog();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

这很好用,但是我的按钮被夹在窗口的底部,因为窗口会自动缩放:

clamping

一旦消息变长,按钮似乎在移动:

moving button

我如何确保按钮保持居中并与底部有大约 10 像素的边距,这样它看起来就不会被夹住?

我尝试手动设置 Margin,但这似乎不起作用。

XAML(主要由设计者生成):

<Window x:Class="RapidEvent.CustomMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RapidEvent"
        mc:Ignorable="d"
        Background="{DynamicResource WindowBackgroundBrush}"
        Title="" Height="Auto" Width="Auto">
    <Grid>
        <StackPanel>
            <Label x:Name="label" Content="" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" FontSize="13px" RenderTransformOrigin="0.392,0.486"/>
            <Button x:Name="button" x:FieldModifier="public" IsDefault="True" Content="_Ok" HorizontalAlignment="Left" Margin="110,40,0,0" VerticalAlignment="Top" Width="80" Height="21" Click="button_Click"/>
        </StackPanel>
    </Grid>
</Window>

最佳答案

只需将 StackPanel 更改为 Grid 并将按钮的 Horizo​​nalAlignment 更改为 Center 并取消所有但底边距。您还需要将 VerticalAlignment 设置为 Bottom。您还需要将按钮放在第 1 行。

这样按钮将固定在对话框的底部并始终居中。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label x:Name="label" Content=""
               HorizontalAlignment="Left" Margin="10,0,0,0"
               VerticalAlignment="Top" FontSize="13px"
               RenderTransformOrigin="0.392,0.486"/>
        <Button Grid.Row="1" x:Name="button" x:FieldModifier="public"
                IsDefault="True" Content="_Ok"
                HorizontalAlignment="Center" Margin="0,0,0,20"
                VerticalAlignment="Bottom" Width="80" Height="21"/>
    </Grid>

关于c# - 使用自动窗口大小设置按钮边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547080/

相关文章:

c# - WPF 组合框不更新 ViewModel

c# - Collapsed 事件在 TreeViewItem 的父节点上触发

WPF Ribbon - 隐藏标签页眉(单标签应用程序)

c# - 为什么我得到一个 "argument out of range exception"?

c# - OpenCV 的 .Net(dotNet)包装器?

c# - 单击在 asp :Repeater 中呈现的 ascx 控件中的按钮时丢失值

c# - 将 TabItem 添加到现有 TabControl WPF/MVVM

wpf - 如何使用 EntityCollection 对象的 HierarchicalDataTemplate 对 WPF TreeView 进行排序?

c# - 如何为 MSChart 创建多种颜色/系列

c# - XAML 找不到命名空间。有时