2014-04-28 108 views
1

我想弄清楚为什么Caliburn.Micro没有绑定我的Screen DisplayName属性到窗口标题开箱。Caliburn Mico:屏幕的ActivateItem.DisplayName属性不会绑定到窗口标题自动

我看了这篇文章: https://stackoverflow.com/a/9072035/2111892 那么,不应该手动绑定它吗?

我的导体是这样的:

[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); 
    } 
} 

...而我的屏幕正在寻找这样的:

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

难道我犯了一个错误还是我理解不正确的东西?

编辑:\

ShellView顺便说一句是这样的:

<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> 

回答

2

标题将绑定到你的ShellViewModel的显示名称,而不是任何的ViewModels创建里面。每当您激活子视图模型时,您都必须更改DisplayName。或者用Title =“{Binding ActiveItem.DisplayName}”明确地绑定它。

+0

喜欢将其绑定到ActiveItem.DisplayName属性:)好吧,这就是我想知道的,所以它不会自动为我做这件事。没关系,那没关系,我会手动绑定它:) – Jannik

0

其实我喜欢下面的解决方案,当你有这样的

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

NullToUnsetConverter导体只是允许结合如果活动项目设置它自己的显示名称为空告吹。如果你不需要这种行为,你可以完全忽略它。