带有字符串绑定到 Int 属性的 WPF 组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18509316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Combobox with string Bind to Int property
提问by Tvd
I want a Combobox with numbers 1-8 and bind the selected value to a property "NumberOfZones" of int type. By default, combobox returns string value so this can't be saved in int property. How do I type cast it to int.
我想要一个数字为 1-8 的组合框,并将所选值绑定到 int 类型的属性“NumberOfZones”。默认情况下,组合框返回字符串值,因此无法将其保存在 int 属性中。我如何键入将其强制转换为 int。
How do I set items and make selection in int.
如何设置项目并在 int 中进行选择。
<ComboBox Background="#FFB7B39D" Height="23" Name="cboNumZones" Width="74" Margin="158,16,368,247" Grid.Row="2" SelectionChanged="cboNumZones_SelectionChanged"
SelectedValue="{Binding Path=NumberOfZones, Mode=TwoWay}">
</ComboBox>
<!--
<ComboBoxItem >1</ComboBoxItem>
<ComboBoxItem >2</ComboBoxItem>
<ComboBoxItem >3</ComboBoxItem>
<ComboBoxItem >4</ComboBoxItem>
<ComboBoxItem >5</ComboBoxItem>
<ComboBoxItem >6</ComboBoxItem>
<ComboBoxItem >7</ComboBoxItem>
<ComboBoxItem >8</ComboBoxItem>
-->
The object that contains NumberOfZones property is the DataContext of the UserControl.
包含 NumberOfZones 属性的对象是 UserControl 的 DataContext。
Many Many Thanks.
非常感谢。
采纳答案by Sheridan
You are mistaken about what a ComboBoxreturns. Yours returns string values because that's what you put into it. If instead you create a property where your NumberOfZonesproperty was declared:
你误会了什么 aComboBox回报。您的返回字符串值,因为这是您放入其中的内容。相反,如果您NumberOfZones在声明您的财产的地方创建了一个财产:
public ObservableCollection<int> Numbers { get; set; }
And then data bind that to your ComboBox:
然后将数据绑定到您的ComboBox:
<ComboBox ItemSource="{Binding Numbers}" Background="#FFB7B39D" Height="23"
Name="cboNumZones" Width="74" Margin="158,16,368,247" Grid.Row="2"
SelectionChanged="cboNumZones_SelectionChanged" SelectedValue="{
Binding Path=NumberOfZones, Mode=TwoWay}">
Then your selected number will be an inttoo.
那么您选择的号码也将是一个int。
回答by dkozl
You can set ItemsSourceas array of int, then SelectedItemwill be of int32type:
您可以设置ItemsSource为 int 数组,然后SelectedItem将是以下int32类型:
<ComboBox SelectedItem="{Binding Path=NumberOfZones, Mode=TwoWay}">
<ComboBox.ItemsSource>
<x:Array Type="{x:Type sys:Int32}">
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
<sys:Int32>4</sys:Int32>
<sys:Int32>5</sys:Int32>
<sys:Int32>6</sys:Int32>
<sys:Int32>7</sys:Int32>
<sys:Int32>8</sys:Int32>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
for this you'll need to add sys:namespace to your XAML:
为此,您需要向sys:XAML添加命名空间:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
回答by Samo
I know the question is for WPF, but just in case you are looking for the answer on Windows 8.1 (WinRT, Universal Apps) it would be:
我知道这个问题是针对 WPF 的,但以防万一您正在 Windows 8.1(WinRT、通用应用程序)上寻找答案,它将是:
<ComboBox SelectedItem="{Binding NumberOfZones, Mode=TwoWay}">
<x:Int32>1</x:Int32>
<x:Int32>2</x:Int32>
<x:Int32>3</x:Int32>
<x:Int32>4</x:Int32>
<x:Int32>5</x:Int32>
</ComboBox>
given that
鉴于
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

