Caliburn Mico: Screen's ActivateItem.DisplayName Property does not bind to Window Title automatically

StackOverflow https://stackoverflow.com/questions/23340787

  •  10-07-2023
  •  | 
  •  

Frage

I am trying to figure out why Caliburn.Micro isnt binding my Screen DisplayName Property to the Window Title out of the box.

I read this post: https://stackoverflow.com/a/9072035/2111892 So, shouldn't it be bound without doing it manually?

My conductor looks like this:

[Export(typeof (ShellViewModel))]
public class ShellViewModel : Conductor<Screen>, IHandle<IViewModelMessage>
{
    [ImportMany(typeof (IViewModelMessageHandler))]
    private IEnumerable<IViewModelMessageHandler> _messageHandlers;

    [ImportingConstructor]
    public ShellViewModel(IEventAggregator eventAggregator)
    {
        eventAggregator.Subscribe(this);
    }

    protected override void OnActivate()
    {
        var viewModel = IoC.Get<LoginViewModel>();
        ActivateItem(viewModel);
    }
}

... while my Screen is looking like this:

[Export(typeof(LoginViewModel))]
public class LoginViewModel : Screen
{
    [ImportingConstructor]
    public LoginViewModel(IEventAggregator eventAggregator, IMessageService messageService, IClient client, IClientReceiver receiver, IClientTransceiver transceiver)
    {
        DisplayName = "Login";
    }
}

Have I made a mistake or did I understand something incorrectly?

Edit:\

ShellView btw looks like this:

<UserControl x:Class="Test.Client.Gui.Views.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="900"
    Width="1000">
<Grid VerticalAlignment="Center">
    <ContentControl x:Name="ActiveItem" />
</Grid>
</UserControl>
War es hilfreich?

Lösung

The Title will bind to the DisplayName of your ShellViewModel, not to any viewmodels you create inside. You will have to change the DisplayName whenever you activate a child view model. Or explicitly bind it with Title="{Binding ActiveItem.DisplayName}".

Andere Tipps

I actually like the following solution when you have a conductor like that

<Window>
    <Window.Title >
        <PriorityBinding>
            <Binding Path="ActiveItem.DisplayName" Converter="{StaticResource NullToUnsetConverter}"></Binding>
            <Binding Path="DisplayName"></Binding>
        </PriorityBinding>
    </Window.Title>
</Window>

NullToUnsetConverter just allows the binding to fall through if the active item sets it's own displayname to null. If you don't need that behaviour you can just omit it entirely.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top