管理 WPF 组合框项目中的数据值 [英] Managing values of data within a WPF Combo Box items
问题描述
我有一个 WPF 组合框,用户必须在其中选择纸张尺寸.例如 cmb_papersize 有一组纸张尺寸的子项:
I have a WPF combo box where the user have to pick between a paper size. For example cmb_papersize has a collection of sub-items of paper sizes:
A3 - 297x420 毫米
A3 - 297x420 mm
F4 - 215x330 毫米
F4 - 215x330 mm
等等...
此组合框输入将链接到确定纸张大小的方法.
This combo box input will be linked to a method to determine the paper size.
通常我会使用 Select...Case 来比较组合框项目索引并设置变量:
Typically I would use a Select...Case where I compare the combo box item index and set the variables:
Private Sub SetPaperSize()
Select Case cmb_papersize.SelectedIndex
Case 0
PageWidth = 297
PageHeight = 420
Case 1
PageWidth = 215
PageHeight = 330
'etc....
End Select
End Sub
但是我很好奇是否有更优雅的方法来做到这一点?使用 Select...Case 管理两个变量可能很简单,但是我计划添加更多变量,如测量单位、边距等.特别是如果我想添加更多纸张尺寸并且每个尺寸肯定会有不同的值.
However I'm curious if there are more elegant way to do this? Managing two variables maybe is simple enough with Select...Case, however I'm planning to add more variables like measurement units, margins, etc.. especially if I want to add more paper size and each sizes will definitely have different values.
我正在考虑使用属性或使用数组,将数据放在不同的类中,等等...但我不知道该怎么做.
I'm thinking to use properties or using arrays, putting the data in different class, etc... but I can't think how should I do it.
推荐答案
基于我上面的评论:
您可以将控件绑定到具有(至少)两个特性.您将 DisplayMemberPath
设置为您希望在控件中显示的属性和 SelectedValuePath
到要通过 SelectedValue
公开的属性的名称.如果后一个属性是具有宽度和高度组件的东西,您可以从 SelectedValue
的控件,获取宽高组件并直接赋值不需要任何 If
的 Select Case
.你可以使用一个System.Windows.Size
值.
You can bind the control to a list of items that have (at least) two properties. You set the
DisplayMemberPath
to the name of the property you want displayed in the control and theSelectedValuePath
to the name of the property you want exposed via theSelectedValue
. If the latter property is something with width and height components, you can get the selected dimensions from theSelectedValue
of the control, get the width and height components and assign them directly without the need for anyIf
ofSelect Case
. You could use aSystem.Windows.Size
value.
这是一个简单的例子:
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim items As New List(Of PaperSize) From {New PaperSize With {.Name = "A3", .Dimensions = New Size(297, 420)},
New PaperSize With {.Name = "F4", .Dimensions = New Size(215, 330)}}
With cmb_papersize
.SelectedValuePath = "Dimensions"
.ItemsSource = items
End With
End Sub
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs)
Dim dimensions = DirectCast(cmb_papersize.SelectedValue, Size)
Dim pageWidth = dimensions.Width
Dim pageHeight = dimensions.Height
MessageBox.Show($"Page dimensions: {pageWidth} by {pageHeight}")
End Sub
End Class
Friend Class PaperSize
Public Property Name As String
Public Property Dimensions As Size
Public Overrides Function ToString() As String
Return $"{Name} - {Dimensions.Width}x{Dimensions.Height} mm"
End Function
End Class
注意,因为我没有设置DisplayMemberPath
,所以控件在每一项上显示调用ToString
的结果,这就是覆盖的原因
.PaperSize
类中的 ToString
Note that, because I haven't set the DisplayMemberPath
, the control displays the result of calling ToString
on each item, which is the reason for overriding ToString
in the PaperSize
class.
这篇关于管理 WPF 组合框项目中的数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!