0

我有一个 xaml(窗口)控件,其中包含一个网格,在该网格内,两个堆栈面板。第二个堆栈面板设置为每个可见性“折叠”。当表单加载并单击表单上的按钮时,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>

主窗口.xaml.cs

public MainWindow()
{
    InitializeComponent();
}

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

0 回答 0