【问题标题】:ComboBox not updating with Itemsource changeComboBox 不随 Itemsource 更改而更新
【发布时间】:2011-07-03 12:38:58
【问题描述】:

我的班级看起来像这样:

Public Class CoursesLib
    Public CoursesOfferedMAIN As New Dictionary(Of String, Category)
    Public Class Category
        Private _CategoryName As String
        Private _Deleted As Boolean
        Public Courses As New Dictionary(Of String, Course)
        Public Function Contains(ByVal CourseName As String)
            For Each k As Course In Courses.Values
                If k.CourseName = CourseName Then
                    Return True
                    Exit Function
                End If
            Next
            Return False
        End Function
    End Class
    Public Class Course
        Private _CategoryName As String
        Private _CourseID As String
        Private _CourseName As String
        Private _Deleted As Boolean
        Public Sems As New Dictionary(Of String, Sem)
        End Sub
        Public Function Contains(ByVal find As String)
            For Each k As Sem In Sems.Values
                If k.SemName = find Then
                    Return True
                    Exit Function
                End If
            Next
            Return False
        End Function
    End Class
End Class

以下是我在 wpf 中用于 xaml 的代码:

<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
            <TextBlock Text="Categories" Margin="0,0,10,0" Width="100" VerticalAlignment="Center" />
            <ComboBox Height="30" Name="CourseCategoryComboBox1"  Width="120">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding CategoryName}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Button Name="AddNewCourseCategoryButton" Background="Transparent" Content="Add New" Foreground="#FF0994EB"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Name="NewCategorySubmitStackPanel">
            <TextBlock Text="Name" Margin="0,0,10,0" Width="100" VerticalAlignment="Center" />
            <TextBox Height="30" Name="NewCourseCategoryTextBox1"  Width="120" MaxLength="25"/>
            <Button Name="SubmitNewCourseCategoryButton" Background="Transparent" Content="+" Margin="10,0,0,0" Foreground="#FF0994EB" FontWeight="Heavy"   BorderBrush="Transparent" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" Name="CourseListStackPanel" >
            <TextBlock Text="Course" Margin="0,0,10,0" Width="100" VerticalAlignment="Center" />
            <ComboBox Height="30" Name="CourseslistComboBox1" Width="120">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding CourseName}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Button Name="NewCourseButton" Background="Transparent" Content="Add New" Foreground="#FF0994EB"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Name="NewCourseeSubmitStackPanel">
            <TextBlock Text="Name" Margin="0,0,10,0" Width="100" VerticalAlignment="Center" />
            <TextBox Height="24" Name="NewCourseeTextBox1"  Width="120" MaxLength="25"/>
            <Button Name="SubmitNewCourseButton" Background="Transparent" Content="+" Margin="10,0,0,0" Foreground="#FF0994EB" FontWeight="Heavy"   BorderBrush="Transparent" />
        </StackPanel>

问题是当向集合中添加新课程时,组合框没有更新,但是当我重新启动应用程序时,它被添加,当我完成插入语句时它没有被插入。以下是我使用的代码。插入和更新控件:

If Not NewCourseeTextBox1.Text = "" Then
        If Globals.Courses.CoursesOfferedMAIN(CType(CourseCategoryComboBox1.SelectedItem, WorkMateLib.CoursesLib.Category).CategoryName).Contains(NewCourseeTextBox1.Text) = False Then
            Dim c As New WorkMateLib.CoursesLib.Course
            c.Category = CType(CourseCategoryComboBox1.SelectedItem, WorkMateLib.CoursesLib.Category).CategoryName
            c.CourseID = DateTime.UtcNow.ToString()
            c.CourseName = NewCourseeTextBox1.Text
            c.Deleted = False
            Dim serv As New ServiceCourses.WCFCoursesClient
            Dim ex As String
            ex = serv.AddCourse(c)
            If ex = "1" Then
                NewCourseeTextBox1.Text = ""
                NewCourseeSubmitStackPanel.Visibility = Windows.Visibility.Collapsed
                Globals.Courses.CoursesOfferedMAIN(c.Category).Courses.Add(c.CourseID, c)
                CourseslistComboBox1.ItemsSource = Globals.Courses.CoursesOfferedMAIN(c.Category).Courses.Values
            Else
                MessageBox.Show(ex)
            End If
        End If
    End If

谢谢。

【问题讨论】:

  • 请在代码块中格式化您的代码并指定语言标签以获得正确的语法着色!
  • 你在哪里绑定/设置ItemsSource
  • @Zebi 我没有使用任何绑定,我只是直接分配源。
  • @Davide Piras,我确实添加了代码,但没有显示,请告诉我,为了被识别为代码,我们只需要在每一行之前添加 4 个空格,请纠正我如果我错了。谢谢。

标签: .net wpf vb.net xaml


【解决方案1】:

字典不提供添加、删除通知使用 ObservableCollection(Of T)。

【讨论】:

  • 你说的可能是对的,但我再次使用了Itemsource,因此必须采用和更新新的来源。
  • 我没有使用任何绑定,我直接分配了字典值。
  • 直接赋值是 VB 6.0 风格 我建议你在 wpf 中使用绑定。如果没有,您最好使用可观察的依赖属性集合。
  • 谢谢@anivas,除了绑定之外,我仍然想查看更多选项,这也是一个不错的选择,但我想检查更多替代方案或知道它为什么不更新。谢谢。
【解决方案2】:

您实际上并没有更改ItemsSource。这一行:

CourseslistComboBox1.ItemsSource = Globals.Courses.CoursesOfferedMAIN(c.Category).Courses.Values

正在将ItemsSource 设置为已分配给它的值:CoursesOfferedMAIN 字典的Values 属性。由于您没有更改值,因此组合框不会做任何事情。

无论如何,将字典的Values 属性用作ItemsSource 并不是一个好主意。字典不会以可预测的顺序维护它们的值,因此它们将在您的 UI 中以基本上随机的顺序出现。

您可能想要创建ValuesCollectionView。 WPF 的CollectionViewSource 对象就是您用来执行此操作的对象。 (请参阅Bea Stollnitz's article,了解为什么需要CollectionViewSource 以及它是如何工作的。)一旦CollectionView 存在,您只需在每次修改它所基于的集合时调用Refresh,并且视图负责排序/过滤和通知 UI。

【讨论】:

  • Collectionview 是一个很好的使用点,然后数据被限制在 1 个或两个类中,我的意思是层次结构,但是,在这种情况下,我有很多类型,它们也像树层次结构,因此按照我的,我认为管理 collectionview 对我来说很困难。谢谢@罗伯特·罗斯尼。这是一篇内容丰富的帖子,我会在情况允许时使用这些信息。
  • Rossney,很抱歉,我只添加了部分代码,因为大多数其他类都相似,但层次结构大约为 5 级深度,因此要发布的代码有点大。
【解决方案3】:

我尝试了几种方法,其中所有以下方法都有效,对我来说是最简单的方法:

诀窍是先将 ItemSource 属性更改为空,然后分配列表或任何其他数据源,这样,项目就会立即显示而没有任何问题。 示例:

感谢您的时间和帮助。

SubjectlistComboBox1.ItemsSource = Nothing
                        SubjectlistComboBox1.ItemsSource = Globals.Courses.CoursesOfferedMAIN(c.Category).Courses(c.CourseID).Sems(c.SemID).Subjects.Values

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2013-04-16
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多