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

时间:2015-12-31 13:19:03

标签: 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

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

我尝试手动设置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>

2 个答案:

答案 0 :(得分:2)

只需将您的StackPanel更改为Grid并将按钮的HorizonalAlignment更改为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>

答案 1 :(得分:2)

使用grid而不是StackPanel:

   <Grid >
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <telerik:Label x:Name="label" Content="LSFFD" 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"  Content="_Ok" HorizontalAlignment="Center" Margin="0 0 0 10" VerticalAlignment="Bottom" Width="80" Height="21" Click="button_Click"/>

        </Grid>