gpt4 book ai didi

wpf - 具有DataTemplateSelector的ContentControl-需要帮助

转载 作者:行者123 更新时间:2023-12-04 13:24:06 24 4
gpt4 key购买 nike

我遇到了一个令人讨厌的问题……也许有人可以(请!)帮忙。
我正在使用一个具有类型枚举的模型和一个应包含枚举中每个选定类型的UI模型的属性:让我们像这样定义它们:

class ViewModel
{
Types selectedType{get;set;}
UiModelBase editedModel{get;set;}
}

我希望有一个内容控件,该控件在每次更改selectedType时都使用datatemplateselector来更改其 View 。
    <ListBox x:Name="RuleTypeList" ItemsSource="{Binding Source={StaticResource Types}}" SelectedItem="{Binding Path=selectedType}"/>     
<!--Content control-->
<ContentControl ContentTemplateSelector="{StaticResource ruleEditTemplateSelector}"
Content="{Binding SelectedItem, ElementName=RuleTypeList}"/>

问题:在我创建的由RuleEditTemplateSelector返回的DataTemplates中
DataContext 类型(同意),但是我需要访问editedModel才能创建我的DataTemplate ...我不知道如何处理

提前致谢!

最佳答案

解决方案不是很难。

 DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContentControl},Path=DataContext}"

这样,模板的上下文与他 parent 的内容相同,我可以同意他的成员。
我想我尝试这样做,但是我没有正确编写代码...
感谢Cstein的参与!

关于wpf - 具有DataTemplateSelector的ContentControl-需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10751419/

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