WPF:数据不会显示在我的数据网格中

时间:2016-08-23 10:53:18

标签: c# wpf xaml datagrid

我正在使用WPF学习XAML,并且我试图在网格中显示一些数据,但我无法让我的生活得到展示。字段显示,但它们在没有文本的情况下为空。这是XAML

    <Window x:Class="MoodWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MoodWPF"
    mc:Ignorable="d"
    Height="810.573" 
    Width="1026.432"
    MinWidth="600">
<Grid x:Name="mainGrid" Background="#333333">
    <DataGrid Width="400" Height="400" x:Name="trackList" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF404040" BorderBrush="{x:Null}" ColumnHeaderHeight="22" GridLinesVisibility="Vertical" RowBackground="#FF404040" AlternatingRowBackground="#FF333333" RowHeight="20" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTemplateColumn x:Name="Track" Header="Track" Width="100" IsReadOnly="True">
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Artist" Width="100" IsReadOnly="True">
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Album" Width="100" IsReadOnly="True">
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Time" Width="100" IsReadOnly="True">
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

和我试图插入一些随机数据的C#

namespace MoodWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


        // create and add three lines of fake data to be displayed, here

        trackList.Items.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });
        trackList.Items.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });
        trackList.Items.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });

        /*
        Collection<DataItem> users = new Collection<DataItem>();
        users.Add(new DataItem { Track = "firstname-1", Artist = "lastname-1" });
        users.Add(new DataItem { Track = "firstname-2", Artist = "lastname-2" });
        users.Add(new DataItem { Track = "firstname-3", Artist = "lastname-3" });
        users.Add(new DataItem { Track = "firstname-4", Artist = "lastname-4" });
        trackList.ItemsSource = users;

    }
}

public class DataItem
{
    public string Track { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
    public string Time { get; set; }
}

}

我做错了什么/不做什么?请记住,我是WPF和Xaml的初学者

3 个答案:

答案 0 :(得分:1)

问题很少,

(i)您需要有一个绑定到DataGrid的集合,因此将项添加到List或ObservableCollection

代码背后:

  public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<DataItem> data = new List<DataItem>();
                data.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });
                data.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });
                data.Add(new DataItem { Track = "a.1", Artist = "a.2", Album = "a.3", Time = "a.4" });
                trackList.ItemsSource = data;
            }

        }
        public class DataItem
        {
            public string Track { get; set; }
            public string Artist { get; set; }
            public string Album { get; set; }
            public string Time { get; set; }
        }

(ii)设置DataGrid AutoGenerateColumns="True"

    

如果设置AutoGenerateColums = false,

<强> XAML:

  <DataGrid Width="400" Height="400" x:Name="trackList" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF404040" BorderBrush="{x:Null}" ColumnHeaderHeight="22" GridLinesVisibility="Vertical" RowBackground="#FF404040" AlternatingRowBackground="#FF333333" RowHeight="20" SelectionMode="Single">
            <DataGrid.Columns>
                <DataGridTextColumn  Binding="{Binding Track}"   x:Name="Track" Header="Track" Width="100" IsReadOnly="True">
                </DataGridTextColumn>
                <DataGridTextColumn  Binding="{Binding Artist}"   Header="Artist" Width="100" IsReadOnly="True">
                </DataGridTextColumn>
                <DataGridTextColumn  Binding="{Binding Album}"   Header="Album" Width="100" IsReadOnly="True">
                </DataGridTextColumn>
                <DataGridTextColumn  Binding="{Binding Time}"  Header="Time" Width="100" IsReadOnly="True">
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

答案 1 :(得分:0)

如果只需要文字,请尝试:

<DataGrid.Columns>
   <DataGridTextColumn x:Name="Track" Width="100" Binding="{Binding Track}" Header="Track"/>
   ...

或者你需要特定的模板:

<DataGridTemplateColumn Header="Artist" Width="100" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding Artist}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

答案 2 :(得分:0)

您必须将模型(可能是特定对象的List)绑定到DataGrid,并将其属性绑定到其相应的列。

种类:

<DataGrid x:Name="TrackList" DataContext="{Binding Source=TrackList}"  AutoGenerateColumns="False">
        <DataGrid.Columns>                
            <DataGridTextColumn Header="Track" Binding="{Binding Track}"/>
            <DataGridTextColumn Header="Artist" Binding="{Binding Artist}"/>
            ...........
            ...........
        </DataGrid.Columns>
</DataGrid>