WPF Combobox with string Bind to Int property
Solution 1
You are mistaken about what a ComboBox
returns. Yours returns string values because that's what you put into it. If instead you create a property where your NumberOfZones
property was declared:
public ObservableCollection<int> Numbers { get; set; }
And then data bind that to your 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 int
too.
Solution 2
You can set ItemsSource
as array of int, then SelectedItem
will be of int32
type:
<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:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Solution 3
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:
<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"
Tvd
Updated on July 27, 2022Comments
-
Tvd almost 2 years
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.
How do I set items and make selection in 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.
Many Many Thanks.
-
Tvd over 10 yearsSheridan, of how wrong I was. Yes your code did the work as you said and even two way was successful. This solution solves my this Q. My other problem - I want to keep the Numbers property common as it is used other places of the app. I am not able to achieve that binding. I have shared it here - stackoverflow.com/questions/18493749/… . Please check out if you can suggest such cool and best option for the same.
-
Tvd over 10 yearsthanks for your response. Sheridan's solution suits more in my app. Thanks.
-
Dave over 2 yearsWill this still work if the program is built 64-bit?