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

1

7

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?

Alcott answered 3/6, 2011 at 17:35 Comment(0)
N
9

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>
Nestling answered 6/6, 2011 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.