【问题标题】:WPF: Hide control in window from child pageWPF:从子页面隐藏窗口中的控件
【发布时间】:2015-03-16 15:54:30
【问题描述】:

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

这是主窗口代码

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

这是不成功的登录页面代码(它在加载的登录页面上运行)

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

【问题讨论】:

  • 你听说过ControlTemplates吗?甚至Triggers?
  • 是的,我有。我不是 WPF @XAMIMAX 方面的专家
  • 看看它们,你会发现在代码后面处理这些事情是一件很痛苦的事情,因为有时必须使用代码后面。

标签: c# wpf xaml


【解决方案1】:

我尝试使用一个界面来处理隐藏主页按钮。参考以下代码。

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。

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    相关资源
    最近更新 更多