简体   繁体   English

WPF:在子页面的窗口中隐藏控件

[英]WPF: Hide control in window from child page

I am using a frame to navigate through multiple pages. 我正在使用一个框架来浏览多个页面。 The window is a wrapper of sorts as it displays the header with the titles to the pages and I have the footer with the navigation controls (back, home, forward). 该窗口是各种包装,因为它显示带有页面标题的标题,而我的页脚带有导航控件(后退,首页,前进)。 However the first page to load is the login screen so I would like to hide/modify the elements in the header and footer. 但是,要加载的第一页是登录屏幕,因此我想隐藏/修改页眉和页脚中的元素。 These items are a part of the main window and the pages are child elements of the main window. 这些项目是主窗口的一部分,页面是主窗口的子元素。 How would I access the main windows elements from the child pages? 如何从子页面访问主要的Windows元素? I've attempted to name the elements and using the FindName method from the child pages but it is not working. 我试图命名元素,并在子页面中使用FindName方法,但是它不起作用。 I have the home button (HomeButtonBorder) collapsed originally and would like to make it visible once the user has successfully logged in. 我最初折叠了主页按钮(HomeButtonBorder),并希望在用户成功登录后使其可见。

Here is the MainWindow Code 这是MainWindow代码

    <Grid>
        <!-- HEADER -->
        <Grid x:Name="HeaderGrid" Style="{StaticResource HeaderGridStyle}">
            <Rectangle x:Name="HeaderRectangle" Style="{StaticResource HeaderRectangleStyle}" />
            <Image x:Name="HeaderLogo" Style="{StaticResource HeaderLogoStyle}" />
            <!-- PAGE HEADER -->
            <!-- TITLE -->
            <Grid x:Name="HeaderTitleGrid" Style="{StaticResource HeaderTitleGridStyle}" >
                <Label x:Name="HeaderTitleLabel"
                       Style="{StaticResource HeaderTitleStyle}"
                       Content="{Binding Path=Content.Title, ElementName=MainFrame}" />
        <!-- END HEADER -->

        <!-- MAIN -->
        <Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
        <!-- END MAIN -->

        <!--NAVIGATION -->
        <!-- Back Button -->
        <Button Content="«" Click="Nav_BackButton_OnClick" HorizontalAlignment="Left"
                Visibility="{Binding Path=CanGoBack, ElementName=MainFrame, Converter={StaticResource BoolToVis}}" 
                Style="{StaticResource NavigationButtonStyle}" />

        <!-- Home Button -->
        <Border x:Name="HomeButtonBorder" Style="{StaticResource HomeBorderStyle}" MouseUp="Nav_HomeButton_OnClick">
            <Image x:Name="HomeImage" Source="/Images/HomeButton_250x250.PNG" Width="100" />
        </Border>

        <!-- Forward Button -->
        <Button Content="»" Click="Nav_ForwardButton_OnClick" HorizontalAlignment="Right"
                Visibility="{Binding Path=CanGoForward, ElementName=MainFrame, Converter={StaticResource BoolToVis}}"
                Style="{StaticResource NavigationButtonStyle}"/>
    </Grid>

Here is the Login Page code that is not successful (It runs on LoginPage loaded) 这是失败的登录页面代码(在已加载的LoginPage上运行)

    public _0_LoginPage()
    {
        InitializeComponent();

    }

    private void LoginLoad(object sender, RoutedEventArgs e)
    {
        var homeButton = FindName("HomeButtonBorder") as Border;
        var homeImage = FindName("HomeImage") as Image;

        if (homeButton == null || homeImage == null) return;
        homeButton.Visibility = Visibility.Visible;
        homeImage.Visibility = Visibility.Visible;
    }

I tried to use a interface which will deal with hiding the Home button. 我试图使用一个界面来处理隐藏“主页”按钮的问题。 Refer the below code. 请参考以下代码。

MainWindow.xaml MainWindow.xaml

<Window x:Class="SQ15Mar2015_Learning.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Content="Login" Click="Button_Click"></Button>
            <Button Content="Home" Visibility="{Binding HomeVisibility, RelativeSource={RelativeSource Mode=FindAncestor,
                                                     AncestorType=Window}}"/>
        </StackPanel>
        <Frame x:Name="frm"></Frame>
    </StackPanel>
</Grid>

MainWindow.cs MainWindow.cs

 public partial class Window2 : Window, IHomeVisibility,INotifyPropertyChanged
{
    public Window2()
    {
        InitializeComponent();
        HomeVisibility = Visibility.Collapsed;
    }

    private Visibility homeVisibility;

    public Visibility HomeVisibility
    {
        get { return homeVisibility; }
        set { homeVisibility = value; OnPropertyChanged("HomeVisibility"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        frm.Navigate(new Login(this));
    }

}

Interface 接口

 public interface IHomeVisibility
{
    Visibility HomeVisibility { get; set; }
}

Login.xaml.cs Login.xaml.cs

public partial class Login : Page
{
    public Login(IHomeVisibility homeBtnVisiblity)
    {
        InitializeComponent();
        homeBtnVisiblity.HomeVisibility = Visibility.Visible;
    }

}

There are many ways of doing dealing it. 有很多处理方法。 I strongly suggest you to go through MVVM. 我强烈建议您通过MVVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM