2012-04-03 76 views
12

我有一个MainView.xaml,绑定到MainViewModel就好了。Caliburn.Micro获取它在MainView中绑定UserControls到他们的ViewModels

我想尝试的是将我在主窗体上的很多控件拆分为UserControls。

现在我将UserControls与MainView一起放入Views文件夹中,并将它们命名为LeftSideControlView.xaml和RightSideControlView.xaml。相应的ViewModels是叫LeftSideControlViewModel中的ViewModels文件夹等

我成功添加用户控件到MAINVIEW:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <UserControls:LeftSideControlView cal:Bind.Model="{Binding}" /> 
    <UserControls:RightSideControlView cal:Bind.Model="{Binding}" 
            Grid.Column="1" /> 
</Grid> 

他们正确地显示在设计师。下面是XAML其中之一:

<UserControl x:Class="TwitterCaliburnWPF.Library.Views.LeftSideControlView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <StackPanel> 
     <Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/> 
     <TextBox x:Name="TextBox1" 
       Width="200" 
       HorizontalAlignment="Center" FontSize="25" Margin="5" /> 
     <Button Width="200" Height="50" Content="Button!" Margin="20" /> 
    </StackPanel> 
</Grid> 

我加入的ViewModels及其接口的AppBootstrapper内使用Castle.Windsor卡利。

public class ApplicationContainer : WindsorContainer 
{ 
    public ApplicationContainer() 
    { 
    // Register all dependencies here 
    Register(
     Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifeStyle.Is(LifestyleType.Singleton), 
     Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Is(LifestyleType.Singleton), 
     Component.For<ILeftSideControlViewModel>().ImplementedBy<LeftSideControlViewModel>(), 
     Component.For<IRightSideControlViewModel>().ImplementedBy<RightSideControlViewModel>() 
     ); 

    RegisterViewModels(); 
    } 

    private void RegisterViewModels() 
    { 
    Register(AllTypes.FromAssembly(GetType().Assembly) 
        .Where(x => x.Name.EndsWith("ViewModel")) 
        .Configure(x => x.LifeStyle.Is(LifestyleType.Transient))); 
    } 

这里的LeftSideControlViewModel类:

using Screen = Caliburn.Micro.Screen; 

namespace TwitterCaliburnWPF.Library.ViewModels 
{ 
    public class LeftSideControlViewModel : Screen, ILeftSideControlViewModel 
    { 
     private string _text = "Hello from the Left side!"; 
     private string _textBox1 = "Enter Text Here"; 

     public string Text 
     { 
      get { return _text; } 
     } 

     public string TextBox1 
     { 
      get { return _textBox1; } 
     } 
    } 
} 

这里的MainViewModel,我要关闭我在Caliburn.Micro机制的文档阅读,因为之前我想这没有什么是特别是在MainViewModel到告诉它加载这两个控件或显示这两个控件。

尽管应用程序启动并运行,但这些值不会绑定到来自其各自视图模型的usercontrols。

namespace TwitterCaliburnWPF.Library.ViewModels 
{ 
    public class MainViewModel : Conductor<IScreen> 
    { 
     public MainViewModel() 
     { 
     ShowLeftControl(); 
     ShowRightControl(); 
     } 

     private void ShowRightControl() 
     { 
     ActivateItem(new RightSideControlViewModel()); 
     } 

     private void ShowLeftControl() 
     { 
     ActivateItem(new LeftSideControlViewModel()); 
     } 

     public string TextToDisplay 
     { 
     get { return "Coming from the ViewModel!"; } 
     } 
    } 
} 

回答

22

这里您不需要使用Conductor。这基本上用于导航场景。只需在您的MainViewModel一个RightSideControlViewModel称为RightSide和一个为LeftSideControlViewModel称为LeftSide两个公共属性。然后,而不是直接在您的MainView中实例化您的用户控件,请创建两个ContentControls,一个用x:Name="LeftSide",另一个用x:name="RightSide"这是实现它的视图模型第一种方法。如果你想这样做,先保持用户控制定义在你的MainView中,但改变Bind.Model,使它指向你创建的新属性,如Bind.Model="{Binding LeftSide}"

基本上,你拥有的东西的方式定义......绑定并不是指向正确的对象,或多或少。你已经得到了指挥,你不需要做到这一点。如果您打算拥有某种导航架构,您可能需要保留它。请记住,当您在Conductor上调用ActivateItem时,您基本上正在更改其ActiveItem属性;一次只有一个模型处于活动状态。在上面的情况下,您激活了这两个项目,但只有第二个项目保持活动状态。此外,在你看来,没有任何东西是绑定到ActiveItem的。

我希望这是有道理的!

相关问题