Я создаю пользовательский элемент управления пользователя. Моя цель - сделать управление повторно используемым. Я использую ItemsControl, вот XAMLМогу ли я привязать/установить свойство зависимостей типа Type к DataType DataTemplate
<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>
Вот управление: 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
Как я могу это достичь?
Спасибо
Это ISN 't необходимо установить DataType здесь, потому что вы явно присвоили DataTemplate свойству ItemTemplate. Нет выбора автоматики по типу данных. – Clemens
Возможно, вам захочется установить ItemTemplate извне. Создайте свойство depedency типа DataTemplate в UserControl и привяжите к нему ItemTemplate ItemsSource. – Clemens
@Clemens Можете ли вы предоставить пример кода? –