WPF更新列表在DataGrid上与其他Thread&Class绑定(WPF Update List binded on DataGrid from other Thread & Class)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
WPF更新列表在DataGrid上与其他Thread&Class绑定(WPF Update List binded on DataGrid from other Thread & Class)

答案:

Application.Current.Dispatcher.Invoke((Action)(() => { logs.Add(this); }));

我尝试为我的服务器创建一个日志系统(Log.cs)

我做了这个类日志:

public class Log { public enum Flags { [Description("SERVER - OK")] OK, [Description("SERVER - ERROR")] ERROR } public string EtatLog { get; set; } public string MessageLog { get; set; } public static ObservableCollection<Log> logs = new ObservableCollection<Log>(); public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; logs.Add(this); } private string GetEnumDescription(Enum enumValue) { var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString(); } }

我已经在我的Datagrid上绑定了我的ObservableCollection,就像那样:

<DataGrid x:Name="GridLogs" Margin="0,5,0,0" ItemsSource="{Binding}" AutoGenerateColumns="False" x:FieldModifier="public"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding EtatLog}" Header="ID du Logs"/> <DataGridTextColumn Binding="{Binding MessageLog}" Header="Message du Logs" Width="*"/> </DataGrid.Columns> </DataGrid>

现在这是我的问题:我想创建一个服务器,所以我使用很多线程来运行它(比如我的Login Listener)。 如果我有一个新的连接(用户)我想在我的列表中写一个日志,所以我这样做:Log log = new Log(Log.Flags.OK,“New co ...”);

但是,我使用多线程,所以我的数据网格无法更新,因为它在主线程上运行...

我有这个错误(法语):

我该如何解决我的问题?

ANSWER :

Application.Current.Dispatcher.Invoke((Action)(() => { logs.Add(this); }));

I try to create a log system for my server (Log.cs)

I made this class log:

public class Log { public enum Flags { [Description("SERVER - OK")] OK, [Description("SERVER - ERROR")] ERROR } public string EtatLog { get; set; } public string MessageLog { get; set; } public static ObservableCollection<Log> logs = new ObservableCollection<Log>(); public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; logs.Add(this); } private string GetEnumDescription(Enum enumValue) { var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString(); } }

I have binded my ObservableCollection on my Datagrid like that :

<DataGrid x:Name="GridLogs" Margin="0,5,0,0" ItemsSource="{Binding}" AutoGenerateColumns="False" x:FieldModifier="public"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding EtatLog}" Header="ID du Logs"/> <DataGridTextColumn Binding="{Binding MessageLog}" Header="Message du Logs" Width="*"/> </DataGrid.Columns> </DataGrid>

Now this is my problem : I want to create a server, so I use many thread to run it (like my Login Listener). If I have a new connection (user) i want to write a log in my list, so i do this : Log log = new Log(Log.Flags.OK, "New co...");

But, I use multi threading, so my datagrid cannot update because it run on the primary thread...

I have this error (in French):

How can I resolve my problem ?

最满意答案

您需要允许主UI线程添加到集合中。

使用Dispatcher

public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; this.Dispatcher.Invoke((Action)(() => { logs.Add(this); } }

You will need to allow the main UI thread to add to the collection.

Use Dispatcher for that

public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; this.Dispatcher.Invoke((Action)(() => { logs.Add(this); } }

更多推荐

本文发布于:2023-04-18 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9d044919d35eebfbf4ccecc8507239cd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   与其他   列表   DataGrid   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!