简体   繁体   中英

MVVM: Bind ContentControl to CheckBox

I have a ContentControl that I want to bind it's Content property to IsChecked property of CheckBox . I am using MVVM, as an idea I thought of doing this:

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

And in the view model I would listen for the IsNewCustumor property to change and assign the corresponding DataTemplate to the CurrentTemplate property, but I think that would involve using views in the view model which is not MVVM . Another idea is to write a converter class, which I don't know how exactly should I implement it.

So can anyone help?

As far as I understand you want to switch the template based on the value of the property IsNewCustomer . One way to achieve this, is using a style trigger. The advantage is, that it is purely XAML and easy to read:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM