I'm building a touch screen application that needs to give the user the ability to quickly copy and paste text in a listview. I've created the menu, but now I am trying to prevent repetitive XAML. I have the following template for the Cell:

<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>

I want to implement it similar to this:

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

I don't necessarily want a "Grid" element, but I need a way to reference the value that is suppose to be entered on that cell.

Grid -> DataTemplate does not exist. Is there another element / tag or another way I should be trying to do this? Am I going about this the wrong way?

有帮助吗?

解决方案

I've gotten the following to work in a way that I think is acceptable. I don't know if it was the best way or not.

In the DefaultResources.XAML, I have:

<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>

Then in the actual list view, I have

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

The ContentPresenter is apparently no longer necessary to remove the button graphic since the "Template" for the button is being set.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top