Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我不能在xaml中定义网格模板吗?_C#_Wpf - Fatal编程技术网

C# 为什么可以';我不能在xaml中定义网格模板吗?

C# 为什么可以';我不能在xaml中定义网格模板吗?,c#,wpf,C#,Wpf,我正在尝试定义一个模板,以便在代码隐藏中使用它 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <Window.Resources> <DataTemplate x:Key="editColumnTemplate">

我正在尝试定义一个模板,以便在代码隐藏中使用它

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <Window.Resources>
        <DataTemplate x:Key="editColumnTemplate">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="deleteButton" Content="X"  Command="{Binding Path=ClickMeCommand}" HorizontalAlignment="Stretch"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <GridView>
        <GridViewColumn Header="Edit" Width="35" CellTemplate="editColumnTemplate"/>
    </GridView>
但我在这行中得到一个错误:

<GridViewColumn Header="Edit" Width="35" CellTemplate="editColumnTemplate"/>

“数据模板”的类型转换器不支持转换 从一根绳子上


DataTemplate是为window定义的资源,因此当您尝试使用它时,它必须引用一个资源

将其定义为静态资源

<GridViewColumn Header="Edit" 
                Width="35" 
                CellTemplate="{StaticResource editColumnTemplate}"/>

这里有很好的解释:

<GridViewColumn Header="Edit" 
                Width="35" 
                CellTemplate="{StaticResource editColumnTemplate}"/>