我的類看起來是這樣的:組合框不會的ItemSource變化更新
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
謝謝。
請在代碼塊中格式化您的代碼並指定語言標記以獲得正確的語法着色! –
你在哪裏綁定/設置'ItemsSource'? – Zebi
@Zebi我沒有使用任何綁定,我只是直接分配源。 – surpavan