簡體   English   中英

MVVM:將ContentControl綁定到CheckBox

[英]MVVM: Bind ContentControl to CheckBox

我有一個ContentControl ,我想將其Content屬性綁定到CheckBox IsChecked屬性。 我正在使用MVVM,作為我想到的一個想法:

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

在視圖模型中,我將監聽IsNewCustumor屬性的更改並將相應的DataTemplate分配給CurrentTemplate屬性,但是我認為這將涉及在不是MVVM的視圖模型中使用視圖。 另一個想法是編寫一個轉換器類,我不知道我應該如何實現它。

有人可以幫忙嗎?

據我了解,您想根據屬性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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM