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}"/>