我正在尝试在WPF数据网格中显示数据列表,因为文本块只是将数据放在一起而没有任何格式化(这是我希望为我完成此数据的格式化)。
这是
背后的代码public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData != null)
{
List<string> mylist = new List<string>();
foreach (var item in e.CmsData.Agents)
{
mylist.Add(item.AgName);
mylist.Add(item.AuxReasonDescription);
}
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
this.datagrid.ItemsSource = mylist;
}));
}
}
}
我用来填充数据网格的XAML
<Grid Height="100" Width="178.201">
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Path=mylist, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" x:Name="datagrid" Background="{x:Null}" Loaded="textBlock_Loaded"/>
</Grid>
这是agents.cs文件,它包含我想要进入我的数据网格的变量,只是需要它。
public class Agent : IEquatable<Agent>
{
public int Extension { get; set; }
public int WorkModeDirection { get; set; }
public string WorkModeDirectionDescription { get; set; }
public TimeSpan AgTime { get; set; }
public int AuxReason { get; set; }
public string AuxReasonDescription { get; set; }
public int DaInQueue { get; set; }
public int WorkSkill { get; set; }
public int OnHold { get; set; }
public int Acd { get; set; }
public String LoginId { get; set; }
public string AgName { get; set; }
public int EId { get; set; }
public int Preference { get; set; }
public DateTime DateTimeCreated { get; set; }
public DateTime DateTimeUpdated { get; set; }
public int CmId { get; set; }
#region Implementation of IEquatable<Agent>
public bool Equals(Agent other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return (other.LoginId == LoginId & other.CmId == CmId);
}
#endregion
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != typeof(Agent))
return false;
return Equals((Agent)obj);
}
//public override int GetHashCode()
//{
// return LoginId;
//}
public override int GetHashCode()
{
string combinedNumber = "" + CmId + LoginId;
int hash = Convert.ToInt32(combinedNumber);
return hash;
}
public static bool operator ==(Agent left, Agent right)
{
return Equals(left, right);
}
public static bool operator !=(Agent left, Agent right)
{
return !Equals(left, right);
}
public override string ToString()
{
return " Ag: [Ext:" + Extension + " login:" + LoginId + " AgName:" + AgName + " CmId:" + CmId + "]";
}
public bool IsValid()
{
return LoginId != null;
}
}
答案 0 :(得分:1)
您应该将ItemsSource
的{{1}}设置为DataGrid
:
List<Agent>
...并为要在XAML标记中的public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData != null)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
this.datagrid.ItemsSource = e.CmsData.Agents.ToList();
}));
}
}
}
中显示的Agent
类的每个属性定义一列:
DataGrid
答案 1 :(得分:0)
您正在尝试将DataGrid绑定到一个根本没有意义的字符串列表。列表中绑定到DataGrid的每个项都应该代表网格中的一行。
由于您尝试在每一行上显示单个代理数据,因此应直接绑定到代理列表。
试试这个;
public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData != null)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
this.datagrid.ItemsSource = e.CmsData.Agents;
}));
}
}
但请记住,这将为Agent对象的每个公共属性生成列。要指定要显示的列,请从另一个SO问题中查看此答案:https://stackoverflow.com/a/6766462/1675709