【问题标题】:WPF: Change Window's Title on every Page NavigationWPF:在每个页面导航上更改窗口的标题
【发布时间】:2017-02-17 06:00:43
【问题描述】:

我有一个 WPF application 由一个窗口 MainWindow.xaml 组成

带有标题Sample_Window

<Window
   Title="Sample_Window">
<Grid>
   <Frame x:Name="mainFrame" Source="/Page1.xaml" />
<Grid>
</Window>

三页:

    Page1.xaml
    Page2.xaml
    Page3.xaml

我的问题是我想在每个 页面导航

上更改 MainWindow 的标题

例如,当我登陆 Page1.xaml 时,标题应该是 Sample_Window-Page1 等。

我在XAML 中尝试了下面的代码:(没用)

<Page 
    WindowTitle="Sample_Window-Page1"
    Title="Page1">
</Page>

另外,在CS中:(没用)

Page1 mypage= new Page1();
mypage.WindowTitle="Sample_Window-Page1";

我也经历过这个问题How to give title to WPF pages

但没有解决我的问题。标题仍与 Sample_Window

相同

【问题讨论】:

    标签: c# wpf xaml window


    【解决方案1】:

    如果您的 Page1 的 Title 是动态生成的(“Customer Joe SmithDetails”),您可能在 Page1ViewModel 上有一个名为 Title 的属性。在这种情况下,您可以简单地从 MainWindow 绑定到 Frame 的 Content 的 DataContext:

    <Window 
      Title="{Binding ElementName=mainFrame, Path=Content.DataContext.Title}">
      <Grid>
        <Frame x:Name="mainFrame" Source="/Page1.xaml" />
      </Grid>
    </Window>
    

    如果您的 Page1 Title 是静态文本(使用 Title 而不是 WindowTitle 属性),则只需从您的主窗口直接绑定到它:

    <Window 
      Title="{Binding ElementName=mainFrame, Path=Content.Title}">
      <Grid>
        <Frame x:Name="mainFrame" Source="/Page1.xaml" />
      </Grid>
    </Window>
    

    【讨论】:

    • 我需要在哪里写这个?在
    • 正如我写的“从你的 MainWindow”它应该放在 中,我会更新答案以使其更清晰。
    • 好吧,它仍然不起作用..这就是我所做的..
    • 如果您使用 DataContext.Title,您必须确保 Page1 具有 DataContext 并且在 DataContext 中有 Title 属性。如果窗口标题是动态的,这很有用。否则只需绑定到 Content.Title(确保 Page1 具有 Title 属性设置)。
    【解决方案2】:

    以上内容对我不起作用,因为它在运行应用程序时加载了 [myPage1]。我通过单击 MainWindow 上的按钮显示 [myPage1],以便显示具有静态标题的页面,我将所需的标题文本添加到导航中。

    MainWindow.xaml

    <DockPanel> <Frame x:Name="_mainFrame"/> </DockPanel>
    

    button_click 方法中的 MainWindow.xaml.cs

    _mainFrame.NavigationService.Navigate(new [myPage1] (Title= "what ever you want displayed"));
    

    【讨论】:

      【解决方案3】:
      this.Title = "Sample_Window-Page1";
      

      你可以试试这个吗?框架不会更改窗口,因此标题保持不变,您应该在每次更改页面时更新窗口的标题。

      【讨论】:

      • 不行,因为this.Title是改变页面标题的属性
      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多