C# ListView通用菜单

C# ListView通用菜单,c#,wpf,listview,datatemplate,C#,Wpf,Listview,Datatemplate,我正在构建一个触摸屏应用程序,它需要让用户能够在listview中快速复制和粘贴文本。我已经创建了菜单,但现在我正在努力防止重复的XAML。我有以下单元格模板: <DataTemplate x:Key="copyPaste"> <Button Click="cell_click" Tag="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" Fo

我正在构建一个触摸屏应用程序,它需要让用户能够在listview中快速复制和粘贴文本。我已经创建了菜单,但现在我正在努力防止重复的XAML。我有以下单元格模板:

<DataTemplate x:Key="copyPaste">
    <Button Click="cell_click" Tag="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <ContentPresenter />
            </ControlTemplate>
        </Button.Template>
        <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" />
    </Button>
</DataTemplate>

我希望实现与此类似的功能:

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <Grid Tag="{Binding Serial}" DataTemplate="{DynamicResource copyPaste}"></Grid>
    </DataTemplate>
</GridViewColumn.CellTemplate>

我不一定需要一个“网格”元素,但我需要一种方法来引用假定输入到该单元格中的值


网格->数据模板不存在。是否有其他元素/标记或其他方法可以尝试这样做?我这样做是错误的吗?

我已经以我认为可以接受的方式完成了以下工作。我不知道这是不是最好的方法

在DefaultResources.XAML中,我有:

<ControlTemplate x:Key="copyPaste" TargetType="Button">
    <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />
</ControlTemplate>

<Style x:Key="copyPasteStyle" TargetType="Button">
    <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"></Setter>
</Style>

然后在实际列表视图中,我有

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <Button Tag="{Binding Serial}" Template="{DynamicResource copyPaste}" Click="cell_click" Style="{DynamicResource copyPasteStyle}" />
    </DataTemplate>
</GridViewColumn.CellTemplate>

ContentPresenter
显然不再需要删除按钮图形,因为正在设置按钮的“模板”