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

时间:2015-03-16 15:54:30

标签: c# wpf xaml

我正在使用框架来浏览多个页面。窗口是各种包装,因为它显示带有页面标题的标题,我的页脚带有导航控件(后退,主页,前进)。但是要加载的第一个页面是登录屏幕,所以我想隐藏/修改页眉和页脚中的元素。这些项是主窗口的一部分,页面是主窗口的子元素。如何从子页面访问主窗口元素?我试图命名元素并使用子页面中的FindName方法,但它不起作用。我的主页按钮(HomeButtonBorder)最初已折叠,并且希望在用户成功登录后使其可见。

这是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>

这是不成功的登录页面代码(它在加载的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;
    }

1 个答案:

答案 0 :(得分:0)

我尝试使用一个处理隐藏Home按钮的界面。请参阅以下代码。

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

 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));
    }

}

接口

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

Login.xaml.cs

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

}

有很多方法可以处理它。我强烈建议你通过MVVM。