Note that there are some explanatory texts on larger screens.

plurals
  1. POCaliburn.Micro getting it to bind UserControls in a MainView to their ViewModels
    primarykey
    data
    text
    <p>I have a MainView.xaml, binding to a MainViewModel just fine.</p> <p>What I wanted to try out was splitting a lot of controls I have on my main form into UserControls.</p> <p>Now I put the UserControls inside the Views folder along with the MainView and named them, LeftSideControlView.xaml and RightSideControlView.xaml. The corresponding ViewModels are in the ViewModels folder called LeftSideControlViewModel, etc.</p> <p>I successfully added the usercontrols to the mainview:</p> <pre><code> &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;UserControls:LeftSideControlView cal:Bind.Model="{Binding}" /&gt; &lt;UserControls:RightSideControlView cal:Bind.Model="{Binding}" Grid.Column="1" /&gt; &lt;/Grid&gt; </code></pre> <p>they show up correctly in the designer. Here's one of them in xaml:</p> <pre><code> &lt;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"&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/&gt; &lt;TextBox x:Name="TextBox1" Width="200" HorizontalAlignment="Center" FontSize="25" Margin="5" /&gt; &lt;Button Width="200" Height="50" Content="Button!" Margin="20" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>I added the viewmodels and their interfaces inside the AppBootstrapper for Caliburn using Castle.Windsor.</p> <pre><code> public class ApplicationContainer : WindsorContainer { public ApplicationContainer() { // Register all dependencies here Register( Component.For&lt;IWindowManager&gt;().ImplementedBy&lt;WindowManager&gt;().LifeStyle.Is(LifestyleType.Singleton), Component.For&lt;IEventAggregator&gt;().ImplementedBy&lt;EventAggregator&gt;().LifeStyle.Is(LifestyleType.Singleton), Component.For&lt;ILeftSideControlViewModel&gt;().ImplementedBy&lt;LeftSideControlViewModel&gt;(), Component.For&lt;IRightSideControlViewModel&gt;().ImplementedBy&lt;RightSideControlViewModel&gt;() ); RegisterViewModels(); } private void RegisterViewModels() { Register(AllTypes.FromAssembly(GetType().Assembly) .Where(x =&gt; x.Name.EndsWith("ViewModel")) .Configure(x =&gt; x.LifeStyle.Is(LifestyleType.Transient))); } </code></pre> <p>Here's the LeftSideControlViewModel class:</p> <pre><code> 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; } } } } </code></pre> <p>Here's the MainViewModel and I'm going off what I read in the Caliburn.Micro documention, as before I tried this nothing was specifically in the MainViewModel to tell it to load these 2 controls or show these 2 controls.</p> <p>Still when the app is up and running the values are not binding to the usercontrols from their respective viewmodels.</p> <pre><code>namespace TwitterCaliburnWPF.Library.ViewModels { public class MainViewModel : Conductor&lt;IScreen&gt; { 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!"; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload