C# 线程池创建 UI 元素

发布于 2024-10-10 09:45:25 字数 2161 浏览 4 评论 0原文

我正在通过数据绑定更新列表框,我尝试插入的列之一是一个复选框。此更新正在由线程池处理,我可以很好地插入数据,除了复选框之外。当我创建复选框时,它显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2024-10-17 09:45:25

您不应将 UI 元素 (CheckBox) 放入 ListView 的数据中,而应定义一个模板,以便可以将该列呈现为复选框,并且只需使用 < code>Boolean 用于数据。在数据中使用 CheckBox 会混合 UI 和数据层。

更新:

以下是如何制作自定义列模板的示例(未经测试):

<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">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=NoteReminderCompleted}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

Rather than putting a UI element (CheckBox) in the data for the ListView, you should define a template so you can render the column as a checkbox, and just use a Boolean for the data. Using a CheckBox 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:

<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">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=NoteReminderCompleted}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>
落日海湾 2024-10-17 09:45:25

您需要使用 bool 字段作为 NoteReminderCompleted 而不是 CheckBox。

You need to use bool field for NoteReminderCompleted instead of CheckBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文