[英]WPF using MVVM Light: How to bind a ContentControl to a child property
[英]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.