我有一个ItemsControl,其ItemsSource绑定到ObservableCollection < Component <!> gt;在运行时。我已经为Component类型定义了一个工作正常的数据模板。

现在Component有一个ObservableCollection <=> Control <!> gt;我想在我的Component Datatemplate中添加另一个ItemsControl来渲染所有控件。这里的控制是我自己的与wpf控件无关的自定义对象。

有不同类型的控件,所以我尝试使用ItemTemplateSelector为每种类型选择正确的模板。在下面的例子中,为了保持它小,我只显示了一个模板<!>“RWString <!>”;我发现在MyControlTemplateSelector中使用FindResource重写SelectTemplate。但是从不调用SelectTemplate(使用断点来检查)。我的xaml有什么问题吗?

<ItemsControl.Resources>
    <src:MyControlTemplateSelector x:Key="XSelector" />
    <DataTemplate DataType="{x:Type src:Component}"  >
        <Expander Visibility="{Binding Path=Show}">
                <ItemsControl ItemsSource="{Binding Path=Contrls}" 
                          ItemTemplateSelector="{StaticResource XSelector}">
                <ItemsControl.Resources>
                    <DataTemplate x:Key="RWstring" >
                        <TextBlock Text="{Binding Path=Label}"/>
                    </DataTemplate>
                </ItemsControl.Resources>
                <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Expander>
    </DataTemplate>
</ItemsControl.Resources>

更新: Contrls不是一个错字,它只是我使用愚蠢的命名系统。 Contrls是ObservableCollection类型的Component的属性。我试图使用ItemsTemplateSelector的原因是ObservableCollection <=> Control <!> gt;包含泛型类型的对象,如Control <=> int <!> gt; <!>控制<=>串GT;所有这些都来自Control,显然你无法创建引用泛型类型的数据模板。

Update3:删除了更新2,因为它不相关。我通过将StaticResource更改为DynamicResource来使ItemTemplateSelector工作。但我不知道为什么会这样......

有帮助吗?

解决方案

我猜这不适用于StaticResource,因为资源在ItemsControl中,在评估StaticResources时可能还没有在加载时创建。

加载时的DynamicResources在加载时计算为表达式,然后在请求时计算为正确的值。

尝试在ItemsControl之外移动资源。

其他提示

在绑定嵌套ItemsControl的行中,Path是否正确?它目前是<!>“Contrls <!>”,如果它是<!>“控件<!>”;?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top