ComboBox未使用Itemssource更新进行更新

时间:2011-07-03 12:38:59

标签: .net wpf vb.net xaml

我的班级看起来像这样:

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>

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

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

谢谢。

3 个答案:

答案 0 :(得分:1)

Dictionary不提供添加,删除通知使用ObservableCollection(Of T)。

答案 1 :(得分:1)

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

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

ItemsSource设置为已分配给它的值:Values字典的CoursesOfferedMAIN属性。由于您尚未更改该值,因此组合框不会执行任何操作。

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

您可能想要创建CollectionView的{​​{1}}。 WPF的Values对象就是您用来执行此操作的对象。 (请参阅Bea Stollnitz's article,详细了解为何需要CollectionViewSource及其工作原理。)CollectionViewSource存在后,每次修改时都只需在其上调用CollectionView它所基于的集合,视图负责排序/过滤和通知UI。

答案 2 :(得分:0)

我尝试了一些方法,其中所有方法都是有效的,对我来说是最简单的方法:

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

感谢您的时间和帮助。

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