StackPanel 可见性变化

StackPanel visibility change

在这里,我试图在页面上单击按钮时显示堆栈面板。使用框架导航页面。 这是我的代码

login.page xaml

<StackPanel Panel.ZIndex="2" Name="pnlLeftMenu" Orientation="Horizontal"  Margin="0,0,-192,0" Width="227" HorizontalAlignment="Right" Visibility="Collapsed" >
        <StackPanel.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="#FF458B85"/>
                <GradientStop Color="#FF458B85" Offset="0.564"/>
            </LinearGradientBrush>
        </StackPanel.Background>
        <Grid Width="25">
            <Image Height="30" Margin="8,0,-9,0" VerticalAlignment="Top" Stretch="None" Source="Images/menu61.png" MouseDown="Image_MouseDown"/>


        </Grid>
        <Grid Width="200" >
            <Grid.RowDefinitions>
                <RowDefinition Height="31*"/>
                <RowDefinition Height="68*"/>
                <RowDefinition Height="178*"/>
                <RowDefinition Height="43*"/>
            </Grid.RowDefinitions>
            <!--<Border Grid.Row="1" Background="Blue" Margin="10,0,0,0"/>-->
            <Border Grid.Row="1" RenderTransformOrigin="0,1" BorderThickness="1,0" CornerRadius="50" Background="Blue" HorizontalAlignment="Left" Width="55" Height="55" VerticalAlignment="Top" Margin="10,0,0,0">
                <Image Source="Images/user.png" Height="35"/>
            </Border>
            <Label  Grid.Row="1" Content="Administrator" Width="130" Height="40" FontWeight="Bold" Foreground="White" FontSize="18" Margin="70,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Border Name="homeBorder" Grid.Row="2" Width="180" Height="30"  Cursor="Hand" Margin="10,10,10,0" VerticalAlignment="Top" >
                <Border.Style>
                    <Style>
                        <Setter Property="Border.Background" Value="#00cc66"/>
                        <Style.Triggers>
                            <Trigger Property="Border.IsMouseOver" Value="True">
                                <Setter Property="Border.Background" Value="#006633"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <Label Content="Home" Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Foreground="White"/>

            </Border>
        </Grid>


    </StackPanel>

我的 C# 代码

public partial class Login : Page
{
    public Login()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        MainWindow window = new MainWindow();

        window.pnlLeftMenu.Visibility = System.Windows.Visibility.Visible;
        Home hPage = new Home();
        this.NavigationService.Navigate(hPage);

    }
}

请帮帮我。

您需要将 MainWindow 的引用传递给登录页面:

public partial class Login : Page
{
    MainWindow _window;

    public Login(MainWindow window)
    {
        InitializeComponent();

        _window = window;
    }

    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        _window.pnlLeftMenu.Visibility = System.Windows.Visibility.Visible;
        Home hPage = new Home();
        this.NavigationService.Navigate(hPage);
    }
}