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

标签 wpf datatemplate contentcontrol datatemplateselector

我遇到了一个令人讨厌的问题……也许有人可以(请!)帮忙。
我正在使用一个具有类型枚举的模型和一个应包含枚举中每个选定类型的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/

相关文章:

WPF:引发页面模式弹出窗口的最佳方式?

wpf - 带有RelativeSource TemplatedParent 的Caliburn.Micro DataTrigger

Wpf 内容控制

c# - WPF 新手 - 如何将整个 Binding 对象存储在 DataGrid 的行中?

c# - 使用 View 模型在 TreeView 中展开路径

wpf - DataTemplate 似乎没有绑定(bind)

C#/WPF : Get Binding Path of an Element in a DataTemplate

mvvm - 自己的 DataTemplateSelector MVVM

c# - 将 ContentControl 绑定(bind)到 UserControl,并重用同一实例

wpf - 多个绑定(bind)到 IsEnabled