2015-04-22 90 views
1

我有一个ContentControl,我想要将它的Content属性与IsChecked属性CheckBox绑定。 我使用MVVM,作为一个想法,我想这样做的:MVVM:将ContentControl绑定到CheckBox

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

并在视图模式,我会听的IsNewCustumor属性更改,并指定相应的DataTemplateCurrentTemplate财产,但我认为,将涉及在视图模型中使用非MVVM的视图。 另一个想法是编写一个转换器类,我不知道我应该如何实现它。

所以任何人都可以帮忙吗?

回答

1

据我了解,您希望根据属性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> 
+0

令人印象深刻的思维方式。谢谢我添加了一些修改,因为我想直接绑定到CheckBox。非常感谢。 –

+0

不客气,我很高兴它帮助! – Marc