16

ok, I must be having a brain freeze here...

I have a ComboBox with 6 items and I'm trying to bind the selected item to an integer value. Its not working, I suspect its because the ComboBoxItem's are strings. I don't feel like making a list in code behind just to fill this little box, so is there a way in xaml to tell the comboboxitems that they are holding integer numbers? Something like <x:Int>2</x:Int> maybe?

xaml:

<ComboBox SelectedItem="{Binding SavedPrintTicket.PagesPerSheet}">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>4</ComboBoxItem>
    <ComboBoxItem>6</ComboBoxItem>
    <ComboBoxItem>8</ComboBoxItem>
    <ComboBoxItem>16</ComboBoxItem>
</ComboBox>
Shaboboo
  • 1,347
  • 1
  • 16
  • 35

1 Answers1

46

Use the System namespace:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

And then your combo-box can contain integers like so:

<ComboBox>
   <sys:Int32>1</sys:Int32>
</ComboBox>
Charlie
  • 15,069
  • 3
  • 64
  • 70
  • I'd like to emphasize that if you don't include the xmlns System namespace, sys:Int32 will be undefined. I've seen other solutions that say to use sys:Int32, but fail to mention the namespace - making it seem like the solution doesn't work or isn't supported somehow. – Tim Holt Jun 14 '23 at 17:35