以下代码在vb.net和Windows Form应用程序中很不错。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim comboSource As New Dictionary(Of String, String)()
comboSource.Add("1", "Sunday")
comboSource.Add("2", "Monday")
comboSource.Add("3", "Tuesday")
comboSource.Add("4", "Wednesday")
comboSource.Add("5", "Thursday")
comboSource.Add("6", "Friday")
comboSource.Add("7", "Saturday")
ComboBox1.DataSource = New BindingSource(comboSource, Nothing)
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Key
Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Value
MessageBox.Show(key & " " & value)
End Sub
End Class
我想将上述代码转换为WPF应用程序。
xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="ComboBox1" Height="20" Width="150"/>
<Button x:Name="Button1" Height="20" Width="80" VerticalAlignment="Top" Content="Click Me"/>
</Grid>
</Window>
vb.net
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim comboSource As New Dictionary(Of String, String)()
comboSource.Add("1", "Sunday")
comboSource.Add("2", "Monday")
comboSource.Add("3", "Tuesday")
comboSource.Add("4", "Wednesday")
comboSource.Add("5", "Thursday")
comboSource.Add("6", "Friday")
comboSource.Add("7", "Saturday")
ComboBox1.DataSource = New BindingSource(comboSource, Nothing)
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"
End Sub
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Key
Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Value
MessageBox.Show(key & " " & value)
End Sub
End Class
链接的图片显示了错误:https://prnt.sc/kcig7j
那么,我该如何解决这些错误?
答案 0 :(得分:2)
WinForms属性的WPF等效项如下:
DataSource -> ItemsSource
DisplayMember -> DisplayMemberPath
ValueMember -> SelectedValuePath
此外,请记住,您可以在代码隐藏或XAML中设置属性,因此您的ComboBox
定义如下所示:
<ComboBox x:Name="ComboBox1" Height="20" Width="150" DisplayMemberPath="Value" SelectedValuePath="Key" />
我个人会发现在代码隐藏中设置ItemsSource
属性更加容易:
Dim comboSource As New Dictionary(Of String, String)()
comboSource.Add("1", "Sunday")
comboSource.Add("2", "Monday")
comboSource.Add("3", "Tuesday")
comboSource.Add("4", "Wednesday")
comboSource.Add("5", "Thursday")
comboSource.Add("6", "Friday")
comboSource.Add("7", "Saturday")
ComboBox1.ItemsSource = comboSource
W.R.T。您的事件处理程序,对KeyValuePair(Of String, String)
进行了两次强制转换;您可能应该只强制转换一次并将强制转换值存储在变量中。但是在这种情况下,您甚至不必强制转换SelectedItem
。相反,您可以将sender
强制转换为ComboBox
并读取Text
和Value
属性:
Dim cmb As ComboBox = sender
MessageBox.Show($"Key: {cmb.SelectedValue}, Value: {cmb.Text}')
还要注意,您可以像使用Integer
一样方便地将String
用作字典键:
Dim days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
Dim comboSource = days
.Select(Function(x, index) (item:=x, index:=index+1))
.ToDictionary(Function(x) x.index, Function(x) x.item)