Not being able to change Title in a Caliburn.Micro Conductor View using MahApps MetroWindow
Asked Answered
H

1

6

I'm doing like so:

<Controls:MetroWindow x:Class="BS.Expert.Client.App.Views.ShellView"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ShowTitleBar="True"
    Title="My Title">

The thing is that this is at the same time a defined main conductor on the main window with which I control navigation through other windows, so I'm not able to inherit from MetroWindow to at least trying change the title in the ViewModel:

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
{
    public ShellViewModel()
    {
        #region mahApps Theme Loading

        var theme = ThemeManager.DetectAppStyle(Application.Current);
        var appTheme = ThemeManager.GetAppTheme(App.Configuration.Theme);
        ThemeManager.ChangeAppStyle(Application.Current, theme.Item2, appTheme);

        #endregion
        //TODO: Changing Title here is not possible ((MetroWindow)this).Title = "No way";
        // Tudo bem na seguinte liña
        LocalizeDictionary.Instance.Culture = new System.Globalization.CultureInfo("pt-BR");
        ShowPageOne();
    }

    public void ShowPageOne()
    {
        ActivateItem(new PageOneViewModel());
    }
}

How should I change the title?

Haddock answered 21/7, 2015 at 13:51 Comment(0)
A
3

When using the MVVM pattern you should never try to set anything on the view directly in the view model like this. Instead using data binding to accomplish this.

So you would have a property on your ShellViewModel with something like:

public string MyTitle
{
    get { return _myTitle; }
    set
    {
        _myTitle = value;
        //Insert your property change code here (not sure of the caliburn micro version)
    }
}

and in your window xaml it would be something like:

<Controls:MetroWindow
    Title="{Binding MyTitle}"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    ...
    >
Anthropologist answered 24/7, 2015 at 14:31 Comment(1)
Inheriting from MetroWindow and setting that Title property isn't what you want to do. Even if you did change the inheritance it would not allow you to change the Title of the view from the view model in this way. The ShellView and ShellViewModel would just be 2 different instances of a MetroWindow. If you want to set the title implement what I described and just set the MyTitle property in the view model to the value you want.Anthropologist

© 2022 - 2024 — McMap. All rights reserved.