简体   繁体   English

从用户控件更改窗口标题

[英]Changing the title of the window from a user control

I have a solution using prism with multiple shells. 我有一个使用多壳棱镜的解决方案。 I've almost got it working, but there is one thing that is stumping me. 我几乎可以正常工作了,但是有一件事困扰着我。 Well, two, but the answer will be the same for both since both are an attribute of the main window. 好了,两个,但是答案都一样,因为两者都是主窗口的属性。

I need to change the Title of the window when I inject a usercontrol into the Shell. 将用户控件注入命令行管理程序时,需要更改窗口的标题。

I'm using ViewModelLocator, IRegionManager, and running all of the navigation through the bootstrapper (Thank you Brian Lagunas for the fantastic pluralsight module, btw) 我正在使用ViewModelLocator,IRangionManager,并通过引导程序运行所有导航(谢谢Brian Brianguns的精彩复数模块,顺便说一句)

What I need to do is change the Title of the Shell window when a new view is injected into the content region. 我需要做的是在将新视图注入到内容区域时更改“外壳程序”窗口的标题。 The views are all created as UserControls. 所有视图均创建为UserControls。

I currently have a standard binding for the Title in my shell.xaml code, 目前,我的shell.xaml代码中具有标题的标准绑定,

   Title="{Binding Title}"

and I'm using some very simple code in my ShellViewModel.cs to set it when the Shell initializes. 并且我在ShellViewModel.cs中使用一些非常简单的代码在Shell初始化时进行设置。

    public string ViewTitle = "<window title here>";
    public string Title
    {
       get { return ViewTitle; }
       set { if (ViewTitle != null) SetProperty(ref ViewTitle, value); }
    }

This is an old question but I am currently working through the same situation. 这是一个老问题,但我目前正在处理相同的情况。 I am relatively new to MVVM with Prism but wanted to record how I resolved this issue in the event someone else stumbled upon this while searching for an answer. 我对使用Prism的MVVM相对陌生,但想记录一下在其他人在寻找答案时偶然发现此问题时如何解决此问题。

  1. Create a new class that inherits from BindableBase and add a title property to it: 创建一个从BindableBase继承的新类,并向其添加title属性:

     public class BindableBaseExtended : BindableBase { private string _mainTitle; public string MainTitle { get { return _mainTitle; } set { _mainTitle = value; } } } 
  2. In your MainWindow (or whatever you are using as a shell), Give your ContentControl a Name 在您的MainWindow(或您用作外壳的任何东西)中,为ContentControl命名

     <ContentControl Grid.Row="1" x:Name="mainContent" prism:RegionManager.RegionName="... 
  3. In your MainWindow(shell) bind to the content control element by name and path of where we will set the title: 在您的MainWindow(shell)中,通过名称和设置标题的路径绑定到内容控件元素:

     <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" Text="{Binding Content.DataContext.MainTitle, ElementName=mainContent}" /> 
  4. For your main content panels that will be feeding the title change, make them inherit from BindableBaseExtended (previously inheriting BindableBase): 对于将要提供标题更改的主要内容面板,使它们继承自BindableBaseExtended(以前继承BindableBase):

     public class ViewBViewModel : BindableBaseExtended 
  5. On instantiation of class (someone navigated there) set your MainTitle property: 在类的实例化(有人在那里导航)时,设置您的MainTitle属性:

     public ViewBViewModel(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator; MainTitle = "View B"; eventAggregator.GetEvent<UpdateTitleEvent>().Subscribe(Updated); } 

Your property will now be feed through your user controls to your shell and will change upon navigation. 现在,您的媒体资源将通过您的用户控件输入到您的Shell中,并在导航时发生变化。 Would love to hear any feedback on how to improve this or point me to where this has already been implemented in a more correct fashion but for now I wanted to share this version in the event any one else is stuck. 很乐意听到任何有关如何改进此问题的反馈,或者将其指向已经以更正确的方式实施的地方,但是目前,我想与其他人分享这个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM