我試圖顯示一個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