使用templateselector

时间:2018-01-17 14:34:33

标签: c# wpf datagrid

如果新添加的行或处于编辑模式,我想更改DataGridTemplateColumn中按钮的行为CellTemplateSelector。 按钮应该标准化为" DELETE"但是当处于编辑模式时,它应该被标记为" INSERT"并呼吁另一种方法。

这是我在其他例子中发现的。但我不知道如何访问DataGridButtonSelector中的DataGrid(行)。

在我的XAML中:

<Window.Resources>
    <local:DataGridButtonSelector x:Key="DataGridButtonSelector" />
</Window.Resources>
...
<DataGrid x:Name="dgMassnahmen" AutoGenerateColumns="False" Margin="10,10,10.667,46.667" ColumnWidth="SizeToCells" RowHeight="62" RowEditEnding="dgMassnahmen_RowEditEnding" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" AddingNewItem="dgMassnahmen_AddingNewItem">
        <DataGrid.Resources>
            <DataTemplate x:Key="insertTemplate">
                <Button x:Name="btnDelete" Content="Einfügen" Click="Button_Click_2" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext, Converter={StaticResource GridViewButtonVisibilityConverter}}"></Button>
            </DataTemplate>
            <DataTemplate x:Key="deleteTemplate">
                <Button x:Name="btnDelete" Content="Entfernen" Click="btnDelete_Click" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext, Converter={StaticResource GridViewButtonVisibilityConverter}}">
                </Button>
            </DataTemplate>
        </DataGrid.Resources>
...
<DataGridTemplateColumn Width="1*" Header="" CellTemplateSelector="{StaticResource DataGridButtonSelector}">
...

和我的DataGridButtonSelector.cs:

class DataGridButtonSelector : DataTemplateSelector
{

    public DataTemplate insertTemplate { get; set; }
    public DataTemplate deleteTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
    // HERE I STUCK!
            if (//THE ROW IS IN EDIT MODE (AND SELECTED))
                return
                    element.FindResource("insertTemplate") as DataTemplate;
            else
                return
                    element.FindResource("deleteTemplate") as DataTemplate;
    }
}

1 个答案:

答案 0 :(得分:0)

您只需使用触发器并在其中设置任何内容:

 <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="agggsdf" ></TextBlock>
                            <Button Name="f"></Button>
                        </StackPanel>
                        <DataTemplate.Triggers>  
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsEditing}" Value="True">
                                    <Setter TargetName="f" Property="Content" Value="remove"></Setter>
                                    <Setter TargetName="f" Property="Style">
                                    <Setter.Value>
                                        <Style TargetType="Button">
                                            <EventSetter Event="Click" Handler="Button_Click"></EventSetter>
                                        </Style>
                                    </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsEditing}" Value="False">
                                <Setter TargetName="f" Property="Content" Value="insert"></Setter>
                                <Setter TargetName="f" Property="Style">
                                    <Setter.Value>
                                        <Style TargetType="Button">
                                            <EventSetter Event="Click" Handler="f_Click"></EventSetter>
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>

                        </DataTemplate.Triggers>
                    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>