gpt4 book ai didi

c# - MVVM:将 ContentControl 绑定(bind)到 CheckBox

转载 作者:行者123 更新时间:2023-12-03 10:35:51 24 4
gpt4 key购买 nike

我有一个 ContentControl我想绑定(bind)它的Content属性(property)给 IsChecked CheckBox 的属性(property).
我正在使用 MVVM,作为我想到的一个想法:

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

在 View 模型中,我会收听 IsNewCustumor属性更改并分配相应的 DataTemplateCurrentTemplate属性,但我认为这将涉及在不是 MVVM 的 View 模型中使用 View .
另一个想法是写一个转换器类,我不知道我应该如何实现它。

那么有人可以帮忙吗?

最佳答案

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

关于c# - MVVM:将 ContentControl 绑定(bind)到 CheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29805428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com