xaml窗口不在中心

时间:2013-04-13 04:29:05

标签: wpf

我有一个xaml(窗口)控件,其中包含一个网格,在该网格内部,有两个stackpanel。每个可见性将第二个堆栈面板设置为“折叠”。加载表单并单击表单上的按钮时,xaml窗口将通过设置WindowStartupLocation =“CenterScreen”显示居中。但是,当单击xaml上的按钮将第二个堆栈面板的可见性设置为可见时,它不再居中。任何人都可以帮助解决此问题或告诉我如何解决此问题吗?

更新:仔细观察后,我认为可能是因为可见性设置为崩溃?我尝试过而不是折叠,隐藏,并且它居中,但是当堆栈面板使用时,列显示为空。

以下是代码:

Form1.cs的

public Form1()
{
     InitializeComponent();
     this.StartPosition = FormStartPosition.CenterScreen;
}

private void button_Click(object sender, EventArgs e)
{
    MainWindow childWindow = new MainWindow();
    childWindow.Show();
}

MainWindw.xaml

<Window x:Class="ChildProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:ChildProject"
    Title="MainWindow" 
    Width="Auto"
    Height="Auto"
    SizeToContent="WidthAndHeight"
    AllowsTransparency="False"
    ResizeMode="NoResize"
    WindowStyle="None"
    WindowStartupLocation="CenterScreen">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"  />
        <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <StackPanel x:Name="stkpnlMain" 
                Grid.Column="0" 
                Grid.Row="0" 
                Orientation="Vertical">
        <Label  Content="This is suppose to display message on right ..." 
                FontStyle="Italic" 
                FontWeight="Bold"/>
        <Button Content="Click Me" 
                Width="75" 
                Margin="5" Click="Button_Click" />
    </StackPanel>
    <StackPanel x:Name="stkpnlDisplay" 
                Grid.Column="1" 
                Grid.Row="0" 
                Margin="5,0,0,0" 
                Orientation="Vertical" 
                Background="YellowGreen" Visibility="Collapsed" >
        <Label Content="... First now shows ..." 
               BorderBrush="Black" 
               BorderThickness="1" 
               Background="Yellow" />
        <Label Content="Hopefully this will work" 
               BorderBrush="Red" 
               BorderThickness="3" 
               Margin="0,5" 
               Background="White" />
    </StackPanel>
    </Grid>

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    stkpnlDisplay.Visibility = Visibility.Visible;
}

0 个答案:

没有答案