[英]ComboBox selected item in windows 8.1 or windows phone 8.1
i have xaml like this : 我有这样的xaml:
<ComboBox x:Name="cbProvince1" HorizontalAlignment="Left" Margin="674,481,0,0" VerticalAlignment="Top" Width="236" TabIndex="10">
<ComboBoxItem Content="Alberta"/>
<ComboBoxItem Content="British Columbia"/>
<ComboBoxItem Content="Manitoba"/>
<ComboBoxItem Content="New Brunswick"/>
<ComboBoxItem Content="Newfoundland and Labrador"/>
<ComboBoxItem Content="Nova Scotia"/>
<ComboBoxItem Content="Ontario"/>
<ComboBoxItem Content="Prince Edward Island"/>
<ComboBoxItem Content="Quebec"/>
<ComboBoxItem Content="Saskatchewan"/>
</ComboBox>
when i do access the the selectedItem to C# i am not able to get the string value i wrote as content 当我确实将selectedItem访问到C#时,我无法获取我写为内容的字符串值
var value = cbProvince1.SelectedItem; var value = cbProvince1.SelectedItem;
i am not able to get value in c# . 我无法在C#中获得价值。
You need to reference the Content
property, not the control itself. 您需要引用
Content
属性,而不是控件本身。 However, as SelectedItem
return an object, yo uneed to cast it to a ComboBoxItem
: 但是,当
SelectedItem
返回一个对象时,无需将其强制转换为ComboBoxItem
:
string content = ((ComboBoxItem)cbProvince1.SelectedItem).Content.ToString();
Instead of making your items ComboBoxItem
and then casting its content you can set it to array of string
and then SelectedItem
will be of string
type. 您可以将其设置为
string
数组,然后再将SelectedItem
设置为string
类型,而不是使您的项目成为ComboBoxItem
,然后投射其内容。 This will also work for Int32
, Double
and other system types. 这也适用于
Int32
, Double
和其他系统类型。
<ComboBox x:Name="cbProvince1" SelectedIndex="1" ...>
<sys:String>Alberta</sys:String>
<sys:String>British Columbia</sys:String>
<sys:String>Manitoba</sys:String>
<sys:String>New Brunswick</sys:String>
<sys:String>Newfoundland and Labrador</sys:String>
<sys:String>Nova Scotia</sys:String>
<sys:String>Ontario</sys:String>
<sys:String>Prince Edward Island</sys:String>
<sys:String>Quebec</sys:String>
<sys:String>Saskatchewan</sys:String>
</ComboBox>
you'll need to define sys
namespace 您需要定义
sys
名称空间
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:sys =“ clr-namespace:System; assembly = mscorlib”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.