GridViewColumnHeader没有出现

时间:2017-08-21 20:37:01

标签: wpf xaml gridviewcolumn

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

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>

代码隐藏:

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构造函数的对象保证包含dataA,dataB和dataC等。

有人可以解释为什么我的GridView中的标题不会出现吗?

1 个答案:

答案 0 :(得分:0)

请尝试使用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>
相关问题