2013-03-08 114 views
1

我创建了一个组合框在XAML这样的:Combobox SelectedValue不显示?

ComboBox x:Name="cbTest" ItemsSource="{Binding}" SelectedValue="{Binding Test, Mode=TwoWay}" HorizontalAlignment="Left" Margin="0,10,0,0" Width="250" SelectionChanged="cbTest_SelectionChanged"/> 

而组合框充满以下ItemSources:

cbTest.ItemsSource = new string[] { "Left", "Right", "Center" }; 

我看到ComboBox中3个字符串,但它不显示我选择的SelectedValue。这是属性:

private short _test; 
public short Test 
{ 
    get 
    { 
     return _test; 
    } 
    set 
    { 
     _test = value; 
     NotifyPropertyChanged(); 
    } 
} 

测试给我以下数据:“左”。所以,我得到的数据,但绑定不起作用!

谢谢!

+0

这可能是问题的错字 - 但你的财产以大写字母牛逼返回_Test和套_TEST以小写T,这将使你的这些症状,特别是如果_Test是您在其他地方定义的实际变量.. – 2013-03-08 10:42:43

+0

另外您的项目是字符串,但Test的数据类型很短,这会导致问题。 – 2013-03-08 10:45:40

+0

@ExceptionGuy,请复制/粘贴您的确切代码。就像Daniel指出的那样,你在帖子中输入的内容有几个错误。 – 2013-03-08 10:50:15

回答

1

问题是你不能将System.String转换为System.Int16(简称),并且由于“左”,“右”,“中心”不是数字,你也不能解析。

尝试使用string为您SelectedValue

private string _test; 
public string Test 
{ 
    get 
    { 
     return _test; 
    } 
    set 
    { 
     _test = value; 
     NotifyPropertyChanged(); 
    } 
} 
+0

这是我选择它们时所做的: 'string Test;如果(cbTest.SelectedIndex == 0) 如果(cbTest.SelectedIndex == 0) { test =“Left”; _vm.Test = test; } else if(cbTest.SelectedIndex == 1) { test =“Right”; _vm.Test = test; } else if(cbTest.SelectedIndex == 2) { test =“Center”; _vm.Test = test; }' – ExceptionGuy 2013-03-08 11:31:22