WPF数据网格未显示数据
本文关键字:数据 显示 数据网 WPF 网格 | 更新日期: 2023-09-27 18:00:12
我有一个数据网格:
<DataGrid Name="messageGrid">
<DataGridTextColumn Header="Revision" Binding="{Binding Message.Revision}"></DataGridTextColumn>
<DataGridTextColumn Header="Opcode" Binding="{Binding Message.Opcode}"></DataGridTextColumn>
<DataGridTextColumn Header="Reason" Binding="{Binding Message.Reason}"></DataGridTextColumn>
<DataGridTextColumn Header="Reaction" Binding="{Binding Message.Reaction}"></DataGridTextColumn>
<DataGridTextColumn Header="Projects" Binding="{Binding Message.Projects}"></DataGridTextColumn>
<DataGridTextColumn Header="Links" Binding="{Binding Message.Links}"></DataGridTextColumn>
<DataGridTextColumn Header="Notes" Binding="{Binding Message.Notes}"></DataGridTextColumn>
</DataGrid>
在预览中,我看不到我想看到的列(Revision、Opcode等),当我运行程序时,我里面没有任何列或数据,为什么会发生这种情况?
p.s我在消息里有数据。修改等等。我检查了
编辑:
我有Message属性的视图模型,我将其用作项目来源:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using YogevAfekaRAPAT.YNIDS.Helpers;
using System.Diagnostics;
using System.Data;
namespace YogevAfekaRAPAT.YNIDS.ViewModels
{
public class MessageDetailsViewModel : NotificationObject
{
#region Private Data Members
private Model.Message message;
private ObservableCollection<Model.MType> types;
//private FileManager.fileManager fileManager;
private Logger.Logger logger;
#endregion
#region Properties
#region Types
private ObservableCollection<Model.MType> _types;
public ObservableCollection<Model.MType> Types
{
get { return _types; }
set
{
if (_types != value)
{
_types = value;
}
}
}
#endregion
#region Message
public Model.Message Message
{
get
{
return message;
}
set
{
if (message == value)
{
return;
}
message = value;
RaisePropertyChanged(() => Message);
}
}
#endregion
#endregion
#region Ctor
public MessageDetailsViewModel(Model.Network network, String messageName)
{
fileManager = FileManager.fileManager.getFileManager();
//Message = network
Message = network.getMessageFromName(messageName);
Debug.WriteLine("Inside: public MessageDetailsViewModel(Model.Network network, String messageName) messageName = " + messageName + " Message.Desc = " + Message.Opcode);
}
#endregion
}
}
您必须在<DataGrid.Columns>
标记下添加Columns
。
<DataGrid>
<DataGrid.Columns>
.... // Add columns here.
</DataGrid.Columns>
</DataGrid>
您忘记将列定义包含在<DataGrid.Columns>
:中
<DataGrid Name="messageGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Revision" Binding="{Binding Message.Revision}" />
<DataGridTextColumn Header="Opcode" Binding="{Binding Message.Opcode}" />
<DataGridTextColumn Header="Reason" Binding="{Binding Message.Reason}" />
<DataGridTextColumn Header="Reaction" Binding="{Binding Message.Reaction}" />
<DataGridTextColumn Header="Projects" Binding="{Binding Message.Projects}" />
<DataGridTextColumn Header="Links" Binding="{Binding Message.Links}" />
<DataGridTextColumn Header="Notes" Binding="{Binding Message.Notes}" />
</DataGrid.Columns>
</DataGrid>
您需要绑定到集合。尝试定义一个只返回Message
的ObservableCollection
。
public ObservableCollection<Model.Message> Messages
{
get { return new ObservableCollection<Model.Message> { Message }; }
}