解决 Caliburn.Micro 中的外部用户控件 x:Name 约定绑定

发布于 2024-11-13 10:19:21 字数 1312 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2024-11-20 10:19:21

您需要在使用控件的地方使用 cal:Bind.Model="{Binding}" ; cal 是 Caliburn.Micro 的 xmlns。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文