我可以将Type类型的依赖项属性绑定/设置为DataTemplate的DataType

时间:2016-11-24 16:07:59

标签: c# wpf dynamic types datatemplate

我正在创建自定义用户控件。我的目标是使控件可重用。 我正在使用ItemsControl,这里是XAML

<ItemsControl ItemsSource="{Binding Path=ItemSource , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MyItemsControlWithButtons}}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type Type=typeOf(DataTemplateType)????}">
                        <ContentControl x:Name="instruction" Content="{Binding Path=DataTemplateControl, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MyItemsControlWithButtons}}}"/>
                        <DataTemplate.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                <Setter Property="Background" Value="#DDDDDD" TargetName="instruction" />
                            </Trigger>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter Property="Background" Value="#EEEEEE" TargetName="instruction" />
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
              </ItemsControl.ItemTemplate>
</ItemsControl>

这是control:MyItemsControlWithButtons

public partial class DraggableItemsControlWithButtons : UserControl
    {
        public DraggableItemsControlWithButtons()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty DataTemplateTypeProperty =
        DependencyProperty.Register(nameof(DataTemplateType), typeof(Type), typeof(DraggableItemsControlWithButtons), new UIPropertyMetadata(null));
        public Type DataTemplateType
        {
            get { return (Type)GetValue(DataTemplateTypeProperty); }
            set { SetValue(DataTemplateTypeProperty, value); }
        }

        public static readonly DependencyProperty ItemSourceProperty =
        DependencyProperty.Register(nameof(ItemSource), typeof(IEnumerable), typeof(DraggableItemsControlWithButtons), new UIPropertyMetadata(null));
        public IEnumerable ItemSource
        {
            get { return (IEnumerable)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        public static readonly DependencyProperty DataTemplateControlProperty =
        DependencyProperty.Register(nameof(DataTemplateControl), typeof(Control), typeof(DraggableItemsControlWithButtons), new UIPropertyMetadata(null));
        public Control DataTemplateControl
        {
            get { return (Control)GetValue(DataTemplateControlProperty); }
            set { SetValue(DataTemplateControlProperty, value); }
        }
    }

如您所见,我想将DataTemplateType绑定/设置为ItemsControl.ItemTemplate的DataTemplate的DataType

我怎样才能实现它?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以通过用DataTemplateControl属性替换ItemTemplate属性来简化您的控制。要完成与ItemsControl的相似性,请将ItemSource属性重命名为ItemsSource

public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register(
        nameof(ItemsSource), typeof(IEnumerable),
        typeof(DraggableItemsControlWithButtons));

public IEnumerable ItemsSource
{
    get { return (IEnumerable)GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}

public static readonly DependencyProperty ItemTemplateProperty =
    DependencyProperty.Register(
        nameof(ItemTemplate), typeof(DataTemplate),
        typeof(DraggableItemsControlWithButtons));

public DataTemplate ItemTemplate
{
    get { return (DataTemplate)GetValue(ItemSourceProperty); }
    set { SetValue(ItemSourceProperty, value); }
}

UserControl的XAML中的ItemsControl如下所示:

<ItemsControl
    ItemsSource="{Binding ItemsSource,
                          RelativeSource={RelativeSource AncestorType=UserControl}}"
    ItemTemplate="{Binding ItemTemplate,
                           RelativeSource={RelativeSource AncestorType=UserControl}}" />

然后,您可以像使用ItemTemplate属性的任何其他控件一样分配ItemTemplate:

<local:DraggableItemsControlWithButtons ItemSource="{Binding ...}">
    <local:DraggableItemsControlWithButtons.ItemTemplate>
        <DataTemplate>
           ...
        </DataTemplate>
    </local:DraggableItemsControlWithButtons.ItemTemplate>
</local:DraggableItemsControlWithButtons>