2013-03-08 59 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