我有一个 WPF 应用程序
,由单个窗口 MainWindow.xaml
标题为Sample_Window
<Window
Title="Sample_Window">
<Grid>
<Frame x:Name="mainFrame" Source="/Page1.xaml" />
<Grid>
</Window>
和三页:
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/