2012-04-03 21 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的。

我希望這是有道理的!

相關問題