C# 线程池创建 UI 元素
我正在通过数据绑定更新列表框,我尝试插入的列之一是一个复选框。此更新正在由线程池处理,我可以很好地插入数据,除了复选框之外。当我创建复选框时,它显示 xaml 而不是复选框元素。 IE
System.Windows.Controls.Checkbox 内容:IsChecked:False
NotesReminderViewDetails
的定义
private struct NotesRemindersViewDetails
{
public string NoteReminderID { get; set; }
public string NoteReminderEnterDate { get; set; }
public string NoteReminderDueDate { get; set; }
public string NoteReminderConents { get; set; }
public CheckBox NoteReminderCompleted { get; set; }
}
这是我用来更新列表视图的代码。 NoteReminderType
是一个包含所有注释/提醒信息的结构。
NoteReminderType noteType = noteReminder.NoteReminderDetails;
NotesRemindersViewDetails noteReminderDetails = new NotesRemindersViewDetails();
noteReminderDetails.NoteReminderID = noteType.UserFriendlyNoteReminderID.ToString();
noteReminderDetails.NoteReminderEnterDate = noteType.InsertionDate.ToShortDateString();
noteReminderDetails.NoteReminderDueDate = noteType.DueDate.ToShortDateString();
noteReminderDetails.NoteReminderConents = noteType.Description;
listViewNotesReminders.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
noteReminderDetails.NoteReminderCompleted = new CheckBox();
listViewNotesReminders.Items.Add(noteReminderDetails);
}));
我需要更改什么才能显示复选框而不是线程池线程中的 xaml?
编辑
这是列表视图的xaml代码
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="20" DisplayMemberBinding="{Binding Path=NoteReminderID}" />
<GridViewColumn Header="Entered Date" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderEnterDate}" />
<GridViewColumn Header="Due Date" Width="75" DisplayMemberBinding="{Binding Path=NoteReminderDueDate}" />
<GridViewColumn Header="Note Contents" Width="300" DisplayMemberBinding="{Binding Path=NoteReminderConents}" />
<GridViewColumn Header="Completed" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderCompleted}" />
</GridView>
</ListView.View>
I am updating a listbox via databind and one of the columns I am trying to insert is a checkbox. This update is being processed by a threadpool and I am able to insert the data fine, except for the checkbox. When I create the checkbox its displaying the xaml instead of the checkbox element.
i.e.
System.Windows.Controls.Checkbox Content: IsChecked:False
Definition of NotesReminderViewDetails
private struct NotesRemindersViewDetails
{
public string NoteReminderID { get; set; }
public string NoteReminderEnterDate { get; set; }
public string NoteReminderDueDate { get; set; }
public string NoteReminderConents { get; set; }
public CheckBox NoteReminderCompleted { get; set; }
}
Here is the code I am using to update the listview. NoteReminderType
is a struct with all the note/reminder information.
NoteReminderType noteType = noteReminder.NoteReminderDetails;
NotesRemindersViewDetails noteReminderDetails = new NotesRemindersViewDetails();
noteReminderDetails.NoteReminderID = noteType.UserFriendlyNoteReminderID.ToString();
noteReminderDetails.NoteReminderEnterDate = noteType.InsertionDate.ToShortDateString();
noteReminderDetails.NoteReminderDueDate = noteType.DueDate.ToShortDateString();
noteReminderDetails.NoteReminderConents = noteType.Description;
listViewNotesReminders.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
noteReminderDetails.NoteReminderCompleted = new CheckBox();
listViewNotesReminders.Items.Add(noteReminderDetails);
}));
What do I need to change to get the checkbox to be displayed instead of the xaml form the threadpool thread?
EDIT
Here is the xaml code for the listview
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="20" DisplayMemberBinding="{Binding Path=NoteReminderID}" />
<GridViewColumn Header="Entered Date" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderEnterDate}" />
<GridViewColumn Header="Due Date" Width="75" DisplayMemberBinding="{Binding Path=NoteReminderDueDate}" />
<GridViewColumn Header="Note Contents" Width="300" DisplayMemberBinding="{Binding Path=NoteReminderConents}" />
<GridViewColumn Header="Completed" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderCompleted}" />
</GridView>
</ListView.View>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应将 UI 元素 (
CheckBox
) 放入ListView
的数据中,而应定义一个模板,以便可以将该列呈现为复选框,并且只需使用 < code>Boolean 用于数据。在数据中使用CheckBox
会混合 UI 和数据层。更新:
以下是如何制作自定义列模板的示例(未经测试):
Rather than putting a UI element (
CheckBox
) in the data for theListView
, you should define a template so you can render the column as a checkbox, and just use aBoolean
for the data. Using aCheckBox
in your data is mixing your UI and data layers.Update:
Here's an example (not tested) of how to make a custom column template:
您需要使用 bool 字段作为 NoteReminderCompleted 而不是 CheckBox。
You need to use bool field for NoteReminderCompleted instead of CheckBox.