Tk1*_*993 2 c# wpf xaml window
我有一个WPF application由一个窗口组成MainWindow.xaml
标题为Sample_Window
<Window
Title="Sample_Window">
<Grid>
<Frame x:Name="mainFrame" Source="/Page1.xaml" />
<Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
和三页:
Page1.xaml
Page2.xaml
Page3.xaml
Run Code Online (Sandbox Code Playgroud)
我的问题是我想在每个页面导航上更改 MainWindow 的标题
例如,当我土地Page1.xaml所有权应该是Sample_Window-Page1等。
我试过下面的一段代码XAML:(没有用)
<Page
WindowTitle="Sample_Window-Page1"
Title="Page1">
</Page>
Run Code Online (Sandbox Code Playgroud)
另外,在CS 中:( 不起作用)
Page1 mypage= new Page1();
mypage.WindowTitle="Sample_Window-Page1";
Run Code Online (Sandbox Code Playgroud)
我也经历过这个问题How to give title to WPF pages
但没有得到解决我的问题。标题仍与Sample_Window相同
如果您的 Page1 标题是动态生成的(“客户Joe Smith详细信息”),您可能在 Page1ViewModel 上有一个名为 Title 的属性。在这种情况下,您可以简单地从 MainWindow 绑定到 Frame 的 Content's DataContext:
<Window
Title="{Binding ElementName=mainFrame, Path=Content.DataContext.Title}">
<Grid>
<Frame x:Name="mainFrame" Source="/Page1.xaml" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
如果您的 Page1 Title 是静态文本(使用 Title 而不是 WindowTitle 属性),那么只需从主窗口直接绑定到它:
<Window
Title="{Binding ElementName=mainFrame, Path=Content.Title}">
<Grid>
<Frame x:Name="mainFrame" Source="/Page1.xaml" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)