Я использую UserControl, где основной элемент управления внутри - это datagrid. Все работает. Однако некоторые строки моего кода являются избыточными, поскольку я реализую один и тот же триггер в каждом из 48 столбцов.DataGridTemplateColumn InvokeCommandAction Template
Как создать шаблон для столбца datagridtemplate следующего фрагмента кода? Единственное отличие в каждом столбце - свойство bound. Все триггеры и форматирование одинаковы.
Буду признателен за вашу помощь!
<DataGridTemplateColumn Header="5" MaxWidth="10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Background="{Binding _5, Converter={StaticResource ResourceKey=hourSlotColorConverter}, UpdateSourceTrigger=PropertyChanged}"
Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.StartCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.LastCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.MouseDragCom}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="" MaxWidth="10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Background="{Binding _5_5, Converter={StaticResource ResourceKey=hourSlotColorConverter}, UpdateSourceTrigger=PropertyChanged}"
Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.StartCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.LastCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.MouseDragCom}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="6" MaxWidth="10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Background="{Binding _6, Converter={StaticResource ResourceKey=hourSlotColorConverter}, UpdateSourceTrigger=PropertyChanged}"
Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.StartCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.LastCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.MouseDragCom}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
EDIT
Так что я последовал предложению Шеридана с некоторыми изменениями, чтобы сделать его короче и проще. Хотя наш руководитель проекта предложил этот метод раньше, я понятия не имел, как его реализовать до ввода Sheridan. Kudos ему
Это код используемого UserControl. То, что он содержит ровно те же триггеры, которые мы использовали в нашем исходном коде выше:
<UserControl x:Class="Widget5.View.DGInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:converter="clr-namespace:Widget5.Converter"
xmlns:dg="clr-namespace:Widget5.View"
mc:Ignorable="d"
d:DesignHeight="20" d:DesignWidth="20">
<UserControl.Resources>
<converter:DataGridInfoToParamConverter x:Key="dgInfoConverter" />
</UserControl.Resources>
<TextBlock Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type dg:DGInfo}}}" Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.StartCellCommand}" >
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.LastCellCommand}" >
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.MouseDragCom}" >
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</UserControl>
И на самом DataGridTemplateColumn, мы просто добавили UserControl (DGInfo) и установить его фон, как мы делали раньше. Из-за этого удалось сохранить тысячи строк.
<DataGrid ...>
...
<DataGridTemplateColumn Header="6" MaxWidth="10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<View:DGInfo Background="{Binding _6, Converter={StaticResource ResourceKey=hourSlotColorConverter}, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
</DataGrid>
@Sheridan - Привет, сэр, вы можете мне помочь? Я отметил вас, поскольку вы оказались одной из самых больших помощи в моем последнем запросе – eemwingg