解决 Caliburn.Micro 中的外部用户控件 x:Name 约定绑定
我想使用 x:Name 绑定通过 Caliburn.Micro 的约定来解析嵌套附属用户控件中的属性绑定。
我们的视图的 UI 非常标准。我们有一个卫星项目,其中包含用户控件,然后使用这些控件在视图中组成 UI,类似于下面的示例:
<UserControl x:Class="Company.CompanyView" ...>
<StackPanel>
<customControls:CompanyNameControl />
<customControls:CompanyAddressControl />
....
</StackPanel>
</UserControl>
此视图的 ViewModel 公开将由组成这些用户控件的组件绑定的属性。
class CompanyViewModel : ...
{
public string CompanyName { get; set; }
public string CompanyAddressNo { get; set; }
public string CompanyAddressStreet { get; set; }
...
}
用户控件通常很简单,但它们在许多不同的视图中被大量重用。下面是一个示例:
<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>
我的理解是,在 Caliburn.Micro 中,x:Name 约定样式绑定仅在存在 ViewModel 的情况下才有效。在这种情况下,UserControl 本身并不是一个视图。它用于组合视图。
有没有一种方法可以将嵌套的附属 UserControl 所组成的视图的绑定解析为 ViewModel?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在使用控件的地方使用 cal:Bind.Model="{Binding}" ; cal 是 Caliburn.Micro 的 xmlns。
You need to use cal:Bind.Model="{Binding}" where you use the control; cal is an xmlns for Caliburn.Micro.