wpf: how to make ComboBoxItems hold integers in xaml

可紊 提交于 2019-12-12 10:29:28

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/1343493/wpf-how-to-make-comboboxitems-hold-integers-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!