Window title is overwritten when using Caliburn's conductor<object> in view model
Asked Answered
C

2

6

New to Caliburn and WPF MVVM, so I may be overlooking something pretty simple and I couldn't find anything searching the web.

Set up a simple wpf project with Caliburn.Micro. Set window title in ShellView.xaml. Works fine. Main MetroWindow displays the title as expcted.

Works fine:

[Export(typeof (IShell))]
public class ShellViewModel : PropertyChangedBased, IShell
{}

But change to:

[Export(typeof (IShell))]
public class ShellViewModel : Conductor<object>
{}

and Window title is the fully qualified name of this ViewModel. ANy help would be appreciated. Thanks.

Cystocarp answered 14/4, 2015 at 23:39 Comment(0)
F
11

You can use it like this:

[Export(typeof (IShell))]
public class ShellViewModel : Conductor<IScreen>
{
    public ShellViewModel()
    {
        DisplayName = "Your window title";
    }
}

In my repository you can find some applications in WPF using Caliburn.Micro, for example:

Fullrigged answered 15/4, 2015 at 0:47 Comment(2)
Thanks for link and suggestion. Still the same. Should have mentioned that I'm using Caliburn.Micro 2.02. Noticed in your bootstrapper you're using 1.5.2.Cystocarp
Could you show your project? I prepared a sample app with the new version and it works. Have you seen this documentation? (I have to go, i'll try to help tomorrow).Fullrigged
C
1

Thanks to Wojciech for pointing me in the right direction.

When ShellViewModel is inheriting PropertyChangeBase and IShell, setting the Title = "Window Title" in ShellView.xaml works. But, when using Caliburn.Micro 2.0.2 and inheriting from Conductor (single screen conductor) the window title is overwritten with the fully-qualified name of the view model (in my case):

FBAGOLDEVALUATOR.APP.VIEWMODELS.SHELLVIEWMODEL

This looks like bug in Caliburn.Micro v2.02, unless I'm missing something.

The workaround: Bind the Title property of the window in .xaml to a public property in the ViewModel. The .xaml line:

Title="{Binding Path=DisplayTitle, Mode=OneWay}" 

The property in ShellViewModel.cs:

    private string _displayTitle;
    public String DisplayTitle
    {
        get
        {
            return _displayTitle;
        }
        set
        {
            if (value.Equals(_displayTitle)) return;
            _displayTitle = value;
            NotifyOfPropertyChange(() => DisplayName);
        }
    }

Then set it in the ShellViewModel constructor:

DisplayTitle = "FBA Gold Evaluator";

That seems to work.

Cystocarp answered 15/4, 2015 at 1:22 Comment(3)
I edited your answer adding a small fix to prevent null reference exception in setter ;-). Are you sure that setting "DisplayName" doesn't work? Did you remove Title from XAML first?Fullrigged
You're right! Removing the Title from XAML and setting DisplayName = "New Title" in the ModelView constructor works great. I totally missed that. I guess trying to learn XAML, WPF, MVVM and Caliburn.Micro at the same time is a bad idea. Sorry I jumped the gun. How do I re-mark you're entry as the answer? Thanks.Cystocarp
Just checked yours as the answer. Thanks again.Cystocarp

© 2022 - 2024 — McMap. All rights reserved.