Nested view/usercontrol binding in Caliburn

If we have a viewmodel like this:

 public class DemoViewModel
    {
        public string Name { get; set; }
        public DemoViewModel()
        {
            Name = "testing";
         }
    }

To bind a nested view inside usercontrol, we use cal:View.Model keyword:

<UserControl x:Class="Demo.UI.Wpf.Views.DemoView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="369" Width="425" xmlns:my="clr-namespace:Demo.UI.Wpf.Views"
        xmlns:cal="http://www.caliburnproject.org"
        >

	<Grid>
        <Label Content="{Binding Name}" Height="28" HorizontalAlignment="Left" Margin="54,66,0,0" Name="label1" VerticalAlignment="Top" />
        <my:SubModuleDemoView HorizontalAlignment="Left" Margin="45,164,0,0" x:Name="subModuleDemoView1" VerticalAlignment="Top" Width="307" cal:View.Model="{Binding this}" />
    </Grid>
</UserControl>

<UserControl x:Class="Demo.UI.Wpf.Views.SubModuleDemoView"
             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>

        <Label Content="{Binding Name}" Height="28" HorizontalAlignment="Left" Margin="140,123,0,0" Name="label1" VerticalAlignment="Top" />

    </Grid>
</UserControl>

The trick is in cal:View.Model, it can bind to “this” current datacontext, it can also bind to a specific property which is a sub viewmodel:

    public class DemoViewModel
    {
        public string Name { get; set; }

        public SubModuleDemoViewModel SubModule { get; set; }
        public DemoViewModel()
        {
            Name = "testing";
            SubModule = new SubModuleDemoViewModel();
        }
    }

        ...
        <my:SubModuleDemoView HorizontalAlignment="Left" Margin="45,164,0,0" x:Name="subModuleDemoView1" VerticalAlignment="Top" Width="307" cal:View.Model="{Binding SubModule}" />
        ...

The label will display the Name property from sub-viewmodel.

I’m still learning xaml, why use

 xmlns:cal="http://www.caliburnproject.org"

in xaml? Instead of

xmlns:cal="clr-namespace:Caliburn.PresentationFramework.ApplicationModel;assembly=Caliburn.PresentationFramework"
Advertisements

2 thoughts on “Nested view/usercontrol binding in Caliburn

  1. Get the machine well-oiled and working as the weeks begin. There’ll be people and possibilities everywhere but few of them will seem to come into order. Desires are strong. Work or health may need a different application or approach. Avoid arguments over money politics or religion. Old contacts may resurface. A little exercise wouldnot go amiss! Don’t forget to get the accounts done in amongst all the ‘horizontal huff and puff. There may be news about financial obligations. fake oakleys free shipping

  2. Just like other physical senses of human body, vision is controlled by our human being. We know exactly how to see clearly by the help of brain, and that is why people survive in the world. If our vision sense can work in an effortless condition like smell and taste senses, eyes are sure to be in good state. But we know the effortless condition of eyes is impossible, we are always trying to see what we can not see by squinting our eyes, then the strain on the eyes are seriously placed. The constantly enforced strain chronically pressed the eye muscles, and finally caused the bad vision. foakleys</a%3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s