GridViewColumnHeader 没有出现

GridViewColumnHeader doesn't appear

我有一个 WPF 表单,其中包含一些 GridViewColumns 来显示数据。但是,即使我在每一列中设置 Header 属性,headers 也不会出现。

XAML:

<Window x:Class="MyProject.ViewModel.MyClassView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:MyProject.ViewModel"        
        Title="Title" Height="210.4" Width="500" ResizeMode="NoResize" 
        WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow">
    <Window.Resources>
        <DataTemplate x:Key="tempA">
            <TextBlock Text="{Binding Path=dataA}"/>
        </DataTemplate>
        <DataTemplate x:Key="tempB">
            <TextBlock Text="{Binding Path=dataB}"/>
        </DataTemplate>
        <DataTemplate x:Key="tempC">
            <TextBlock Text="{Binding Path=dataC}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="5,5,5,5">
        <ListView x:Name="MyList" ItemsSource="{Binding MyDataCollection}" MouseDoubleClick="ListView_MouseDoubleClick">
            <ListView.View>
                <GridView>
                    <GridViewColumn x:Name="colA" Header="Name" Width="100" CellTemplate="{StaticResource tempA}"/>
                    <GridViewColumn x:Name="colB" Header="Type" Width="100" CellTemplate="{StaticResource tempB}"/>
                    <GridViewColumn x:Name="colC" Header="Diameter" Width="100" CellTemplate="{StaticResource tempC}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

Code-behind:

namespace MyProject.ViewModel
{
    public partial class MyClassView : Window
    {
        public object SelectedItem = null;

        public MyClassView()
        {
            InitializeComponent();
        }

        private void ListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            SelectedItem = MyList.SelectedItem;
        }
    }
}

视图模型:

namespace MyProject.ViewModel
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public string Type { get; set; }
        public object MyDataCollection { get; set; }

        public MyViewModel(object collection, string type)
        {
            Type = type;
            MyDataCollection = collection;
        }

        internal void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

传递给 ViewModel 构造函数的 object 保证包含 dataA、dataB 和 dataC 等。

有人可以解释为什么我的 GridView 中的 headers 没有出现吗?

请尝试使用 DataGrid

看起来像这样:

<DataGrid ItemsSource="{Binding MyDataCollection}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
        <DataGridComboBoxColumn Header="Type" ItemsSource="{Binding TypeList}" SelectedItemBinding="{Binding Type}"/>
        ...
    </DataGrid.Columns>
</DataGrid>