开发者

ComboBox not updating with Itemsource change

My class Looks like this:

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

Following is the code i used for xaml in my wpf:

<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" FontWeigh开发者_如何学Pythont="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>

The problem is when the add a new course to the collection, the combox is not updating, but when i restart the app, it gets added, it is not getting inserted when I complete the insert statement. Following is the code i use. Inserting and Updating the control:

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

Thank you.


Dictionary does not provide add, remove notification use ObservableCollection(Of T).


You're not actually changing the ItemsSource. This line:

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

is setting the ItemsSource to the value that it is already assigned to: the Values property of the CoursesOfferedMAIN dictionary. Since you haven't changed the value, the combo box doesn't do anything.

It's not a very good idea to use a dictionary's Values property as the ItemsSource anyway. Dictionaries don't maintain their values in a predictable order, and so they'll be appearing in an essentially random order in your UI.

You probably want to create a CollectionView of the Values. WPF's CollectionViewSource object is what you'd use to do this. (See Bea Stollnitz's article for a very good introduction to why CollectionViewSource is needed and how it works.) Once the CollectionView exists, you simply call Refresh on it every time you modify the collection that it's based on, and the view takes care of sorting/filtering and notifying the UI.


I tried a few methods and of them all following worked and is the easiest one as for me:

The trick is to change the ItemSource Property to nothing first and then assigning the list or any other data source, this way, the Items are getting displayed right away without any problem. Example:

Thank you for your time and help.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜