我试图显示一个WPF数据网格中的数据列表,因为textblock只是将数据放在一起没有任何格式(这是我希望格式化这数据为我完成)。试图显示数据网格中的数据列表
这背后
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;
}
}
这是目前正在显示的内容......其中没有包含任何我要它打印出来的信息,我不知道数字来自哪里。
你想一个DataGrid绑定到字符串列表。这没有意义。你是否试图每行显示一个Agent对象? – Hasan
是的,我希望第一列是代理人名称,第二列是agentauxdescription等,等等我尝试添加其他可用变量。我已经尝试过使用textblock和richtextblock,并且失败了。一位同事建议我试试这个。 – mcavanaugh418
取而代之的是将你的项目作为一个对象,并将其添加到可观察的集合中,并将其绑定到您的datagrid项目源 – tabby