我有一个datagrid:
<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>
在预览中,我没有看到我想看到的列(修订版,操作码等),当我运行程序时,我没有任何列或数据,为什么会这样发生了什么?
P.S 我在Message.Revision中有数据,依此类推,我检查了
修改:
视图模型,其中我有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
}
}
答案 0 :(得分:1)
您忘记将列定义括在<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>
您需要绑定到集合。尝试定义仅返回ObservableCollection
。{/ p>的Message
public ObservableCollection<Model.Message> Messages
{
get { return new ObservableCollection<Model.Message> { Message }; }
}
答案 1 :(得分:1)
您必须在 Columns
标记下添加 <DataGrid.Columns>
。
<DataGrid>
<DataGrid.Columns>
.... // Add columns here.
</DataGrid.Columns>
</DataGrid>