c# - WPF:更改每个页面导航上的窗口标题

标签 c# wpf xaml window

我有一个 WPF 应用程序,由单个窗口 MainWindow.xaml

组成

标题为Sample_Window

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

See this image

和三页:

    Page1.xaml
    Page2.xaml
    Page3.xaml

我的问题是我想更改每个页面导航上的主窗口标题

例如,当我登陆 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

相同

最佳答案

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

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

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

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

关于c# - WPF:更改每个页面导航上的窗口标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290477/

相关文章:

c# - 两个 List<FileInfo> 之间的区别

c# - Datagrid 没有双击编辑列

c# - 一旦第二个窗口关闭,属性就会恢复为原始值

c# - WPF 按钮图像源绑定(bind)字符串依赖属性

wpf - ListBoxItem.Parent 不返回任何内容,也无法通过 VisualTreeHelper.GetParent 获取它

c# null for System.drawing.PointT

c# - C# 中的回调

c# - 如何在 wpf 中比较 if((sender as Grid).Background== new SolidColorBrush(Colors.Green))

wpf - 使用 WPF 用户控件和 MVVM

c# - 如何创建自定义属性?