MVVM:将 ContentControl 绑定到 CheckBox

MVVM: Bind ContentControl to CheckBox

我有一个 ContentControl,我想将它的 Content 属性 绑定到 CheckBoxIsChecked 属性。 我正在使用 MVVM,作为我想到这样做的想法:

<ContentControl ContentTemplate="{Binding CurrentTemplate}"/>
<CheckBox IsChecked="{Binding IsNewCustumor}"/>

并且在视图模型中,我会监听 IsNewCustumor 属性 更改并将相应的 DataTemplate 分配给 CurrentTemplate 属性,但是我认为这将涉及在不是 MVVM 的视图模型中使用视图。 另一个想法是写一个转换器class,我不知道我应该如何实现它。

所以有人可以帮忙吗?

据我了解,您想根据 属性 IsNewCustomer 的值切换模板。实现此目的的一种方法是使用样式触发器。优点是,它纯粹是 XAML 并且易于阅读:

<ContentControl>
    <ContentControl.Style>
        <Style TargetType="ContentControl>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsNewCustomer}" Value="True">
                    <Setter Property="ContentTemplate" Value="Set the template for new customers here">
                </DataTrigger>
            </Style.Triggers>
         <Setter Property="ContentTemplate" Value="Set the template for not new customers here">
        </Style>
    <ContentControl.Style>
<ContentControl>