Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving External User Control x:Name Convention Bindings in Caliburn.Micro

I'd like to use x:Name binding to resolve property bindings in nested, satellite user controls via Caliburn.Micro's conventions.

The UI for our Views is pretty standard. We have a satellite project that contains user controls which are then used to compose the UI in our Views, similar to the example below:

<UserControl x:Class="Company.CompanyView" ...>
    <StackPanel>
        <customControls:CompanyNameControl />
        <customControls:CompanyAddressControl />
        ....
    </StackPanel>
</UserControl>

The ViewModel for this View exposes properties that will be bound to by the components that make up these user controls.

class CompanyViewModel : ...
{
    public string CompanyName { get; set; }
    public string CompanyAddressNo { get; set; }
    public string CompanyAddressStreet { get; set; }
    ...
}

The user controls are typically simple, but they are heavily reused in many different views. Here's an example of what one may look like:

<UserControl x:Class="CustomControls.CompanyNameControl ...>
    <StackPanel Orientation="Horizontal">            
        <TextBlock Text="Company Name: " />
        <TextBox x:Name="CompanyName" />  <!--This is how I'd like to bind-->
        <TextBox Text="{Binding CompanyName}" /> <!--This is how I currently bind-->
    </StackPanel>
</UserControl>

My understanding is that in Caliburn.Micro, x:Name convention-style binding only works if there is a ViewModel for the View. In this case, the UserControl is not, itself, a View. It's used to compose the View.

Is there a way to make the binding resolve to the ViewModel for the View upon which the nested, satellite UserControl is composed?

like image 533
Adrian Avatar asked Jun 03 '11 17:06

Adrian


1 Answers

You need to use cal:Bind.Model="{Binding}" where you use the control; cal is an xmlns for Caliburn.Micro.

<UserControl x:Class="Company.CompanyView" ...>
    <StackPanel>
        <customControls:CompanyNameControl cal:Bind.Model="{Binding}"/>
        <customControls:CompanyAddressControl cal:Bind.Model="{Binding}"/>
        ....
    </StackPanel>
</UserControl>
like image 143
phantum66 Avatar answered Sep 27 '22 23:09

phantum66