WPF UserControl具有基于集合类型的动态标头

时间:2014-10-23 19:30:13

标签: wpf vb.net visual-studio-2010 xaml user-controls

我正在创建一个可重用的WPF UserControl ,其中包含一个在代码隐藏中受数据限制的控件。我要使用的控制类型取决于此问题的可能解决方案。 目前,我只使用HeaderedItemsControl

本质上,这个数据绑定控件需要能够处理多种类型的集合,并使用动态生成的标题来适当地显示结果。这样,我可以将一个UserControl用于我需要的所有集合类型,而不是为每个集合类型(或类似集合类型的组)创建UserControl。

我尝试使用 TemplateSelector 类来控制 HeaderTemplateSelector ItemTemplateSelector 属性。当ItemTemplateSelector按预期工作时,由于某种原因 HeaderDataTemplateSelector.SelectTemplate函数永远不会触发

所以,我需要做以下其中一项:

  • 获取HeaderTemplateSelector.SelectTemplate以在绑定期间触发
  • 使用其他方法显示我的自定义集合类
  • 抛弃此尝试,只为每个集合类型创建一个UserControl

感谢任何见解。

相关代码如下。

UserControl XAML内容:

<UserControl.Resources>
    <Local:HeaderDataTemplateSelector x:Key="HeaderDataTemplateSelector" />
    <Local:ItemDataTemplateSelector x:Key="ItemDataTemplateSelector" />
    <DataTemplate x:Key="ProductHeader">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <GridViewColumnHeader Grid.Row="0" Grid.Column="0" Content="SKU" />
            <GridViewColumnHeader Grid.Row="0" Grid.Column="1" Content="Title" />
            <GridViewColumnHeader Grid.Row="0" Grid.Column="2" Content="Price" />
            <GridViewColumnHeader Grid.Row="0" Grid.Column="3" Content="Quantity" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="Product">
        <Border Style="{DynamicResource ResourceKey=ListBoxItemOuterBorder}">
            <Border Style="{DynamicResource ResourceKey=ListBoxItemInnerBorder}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=SKU}" />
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Title}" />
                    <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Path=Price}" />
                    <TextBlock Grid.Row="0" Grid.Column="3" Text="{Binding Path=Quantity}" />
                </Grid>
            </Border>
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="ProductCategoryHeader">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <GridViewColumnHeader Grid.Row="0" Grid.Column="0" Content="Name" />
            <GridViewColumnHeader Grid.Row="0" Grid.Column="1" Content="Parent" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="ProductCategory">
        <Border Style="{DynamicResource ResourceKey=ListBoxItemOuterBorder}">
            <Border Style="{DynamicResource ResourceKey=ListBoxItemInnerBorder}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=Name}" />
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Parent.Name}" />
                </Grid>
            </Border>
        </Border>
    </DataTemplate>
    <!-- more DataTemplates will be created for each data type -->
</UserControl.Resources>
<HeaderedItemsControl Name="Test" HeaderTemplateSelector="{StaticResource ResourceKey=HeaderDataTemplateSelector}"
    ItemTemplateSelector="{StaticResource ResourceKey=ItemDataTemplateSelector}">

HeaderDataTemplateSelector代码:

NotInheritable Class HeaderDataTemplateSelector
    Inherits DataTemplateSelector
    Public Overrides Function SelectTemplate(item As Object, container As System.Windows.DependencyObject) As DataTemplate
        Dim Element As FrameworkElement = CType(container, FrameworkElement)
        If item IsNot Nothing AndAlso Element IsNot Nothing Then
            Return Element.TryFindResource(item.GetType.Name & "Header")
        Else
            Return Nothing
        End If
    End Function
End Class

ItemDataTemplateSelector代码:

NotInheritable Class ItemDataTemplateSelector
    Inherits DataTemplateSelector
    Public Overrides Function SelectTemplate(item As Object, container As System.Windows.DependencyObject) As DataTemplate
        Dim Element As FrameworkElement = CType(container, FrameworkElement)
        If item IsNot Nothing AndAlso Element IsNot Nothing Then
            Return Element.TryFindResource(item.GetType.Name)
        Else
            Return Nothing
        End If
    End Function
End Class

0 个答案:

没有答案