我在WPF应用程序中有一个绑定ItemSource的DataGrid。当这个ItemSource收到更新时,每三秒钟,DataGrid上的排序就会被抛出。它接近我可以从我们的呼叫系统获得的实时馈送,因此需要每三秒更新一次。问题是如何在更新到达DataGrid之前让排序保持或重新应用,以便用户看不到它排序不正确。我在下面发布了一些代码,但不知道您可能需要哪些信息来帮助解决此问题。我整天都在和它斗争。
目标:目前代码程序对此列表进行排序。它如上所述工作三秒钟,但是以程序方式对其进行排序不是目标。我在这里因为我需要帮助弄清楚它为什么不能在数据网格上排序。我希望用户能够从长远来看将它们排序。每次更新项目来源时,我都需要停止离开。
代码背后:
private void textBlock_Loaded(object sender, RoutedEventArgs e)
{
FeedServiceAgent data = new FeedServiceAgent();
data.MessageReceived += OnMessageReceived;
data.Subscribe("92", 3);
}
private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
List<NewAgent> newAgentList = new List<NewAgent>();
if (e == null)
return;
if (e.CmsData != null)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
foreach (var item in e.CmsData.Agents)
{
NewAgent newAgents = new ScoreBoardClientTest.NewAgent();
newAgents.AgentName = item.AgName;
newAgents.AgentExtension = item.Extension;
newAgents.AgentDateTimeChange = ConvertedDateTimeUpdated;
newAgents.AuxReasons = item.AuxReasonDescription;
newAgents.LoginIdentifier = item.LoginId;
newAgents.AgentState = item.WorkModeDirectionDescription;
var timeSpanSince = DateTime.Now - item.DateTimeUpdated;
newAgents.AgentDateTimeStateChange = timeSpanSince;
newAgentList.Add(newAgents);
ListCollectionView myListCollectionView = new ListCollectionView(newAgentList);
myListCollectionView.SortDescriptions.Add(new SortDescription("AgentState", ListSortDirection.Ascending));
myListCollectionView.SortDescriptions.Add(new SortDescription("AuxReasons", ListSortDirection.Descending));
myListCollectionView.SortDescriptions.Add(new SortDescription("AgentDateTimeStateChange", ListSortDirection.Descending));
DataGrid.ItemsSource = myListCollectionView;
}
}));
}
}
XAML:
<DataGrid Name="DataGrid" AutoGenerateColumns="False" IsReadOnly="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" ColumnWidth="*" Foreground="Green" FontSize="10" Loaded="textBlock_Loaded" Margin="0,10" RenderTransformOrigin="0.127,0.275" Background="{x:Null}" IsSynchronizedWithCurrentItem="False" GridLinesVisibility="Horizontal">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding AgentName}" />
<DataGridTextColumn Header="State" Binding="{Binding AgentState}" />
<DataGridTextColumn Header="Aux" Binding="{Binding AuxReasons}" />
<DataGridTextColumn Header="Time" Binding="{Binding AgentDateTimeStateChange, StringFormat={}{0:hh}:{0:mm}:{0:ss}}"/>
</DataGrid.Columns>
</DataGrid>
列表对象的CS:
class NewAgent
{
public string AgentName { get; set; }
public int AgentExtension { get; set; }
public TimeSpan AgentDateTimeChange { get; set; }
public TimeSpan AgentDateTimeStateChange { get; set; }
public String AuxReasons { get; set; }
public string LoginIdentifier { get; set; }
public string AgentState { get; set; }
}
答案 0 :(得分:1)
我的解决方案(不优雅但应该有效) - 在设置新的ItemSource之前保存dataGrid.Items.SortDescriptions
。
删除代码中您执行myListCollectionView.SortDescriptions.Add
的行并尝试执行
var sorting = DataGrid.Items.SortDescriptions
.Select(x => new {x.PropertyName, x.Direction}).ToList();
DataGrid.ItemsSource = myListCollectionView;
foreach (var item in sorting)
{
var col = DataGrid.Columns
.First(x => x.SortMemberPath == item.PropertyName);
col.SortDirection = item.Direction;
DataGrid.Items.SortDescriptions.Add(
new SortDescription(item.PropertyName, item.Direction));
};