X External User Control Resolution: Assigning Symbols to Caliburn.Micro

I would like to use the x: Name binding to enable property binding in nested user satellite controls through the Caliburn.Micro conventions.

The user interface for our views is pretty standard. We have a satellite project that contains user controls that are then used to compose the user interface in our views, as shown below:

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

The ViewModel for this view provides properties that will be bound 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; } ... } 

User controls are usually simple, but they are reused in many different views. Here is an example of what might 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, the 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 was used to create the presentation.

Is there a way to enable binding to the ViewModel for the view on which the nested UserControl satellite is composed?

+6
source share
1 answer

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

 <UserControl x:Class="Company.CompanyView" ...> <StackPanel> <customControls:CompanyNameControl cal:Bind.Model="{Binding}"/> <customControls:CompanyAddressControl cal:Bind.Model="{Binding}"/> .... </StackPanel> </UserControl> 
+8
source

Source: https://habr.com/ru/post/889723/


All Articles