具有DataTemplateSelector的ContentControl-需要帮助 [英] ContentControl with DataTemplateSelector - help needed
问题描述
我遇到了一个棘手的问题……也许有人可以(请!)帮忙。
我正在使用一个具有类型枚举的模型和一个应包含枚举中每个选定类型的UI模型的属性:让我们像这样定义它们:
I got an enoying problem... Maybe someone can (please!) help. I am using a model that has and enumeration of types and a property that should hold UI models for each selected type from enumeration: Let's define them like:
class ViewModel
{
Types selectedType{get;set;}
UiModelBase editedModel{get;set;}
}
我希望有一个内容控件,每次我更改selectedType时,该控件都使用datatemplateselector来更改其视图。
I want to have a content control that use datatemplateselector to change his view each time I change the selectedType.
<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 是 Type (同意),但是我需要访问editedModel来创建我的DataTemplate ...我不知道如何处理
the PROBLEM: In DataTemplates that I create to be returned by ruleEditTemplateSelector the DataContext is Type (agree with that) but I need access to the editedModel to create my DataTemplate...I do not know how to deal with it
谢谢!
推荐答案
解决方案不是很难....
The solution was not very hard....
DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContentControl},Path=DataContext}"
这样,模板的上下文与他父母的内容相同,我可以加入他的成员。
我想我想这样做,但是我没有编写正确的代码...
感谢Cstein的参与!
In this way the context of template is the same with the content of his parent and I can acces his members. I think I tryind to do that but I didn't code correct... Thanks Cstein for involving !
这篇关于具有DataTemplateSelector的ContentControl-需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!