C# 组合框SelectedValue不';你不来吗?

C# 组合框SelectedValue不';你不来吗?,c#,combobox,microsoft-metro,store,C#,Combobox,Microsoft Metro,Store,我在xaml中创建了一个组合框,如下所示: 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.

我在xaml中创建了一个组合框,如下所示:

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" };
我在组合框中看到了3个字符串,但它没有显示我之前选择的SelectedValue。这是财产:

private short _test;
public short Test
{
    get
    {
        return _test;
    }
    set
    {
        _test = value;
        NotifyPropertyChanged();
    }
}
测试给我以下数据:“左”。所以,我得到了数据,但绑定不起作用


谢谢

问题是您无法将
System.String
转换为
System.Int16
(short),并且您也无法解析,因为“Left”、“Right”、“Center”不是数字

尝试使用
string
作为您的
SelectedValue

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

问题是您无法将
System.String
转换为
System.Int16
(short),并且您也无法解析,因为“Left”、“Right”、“Center”不是数字

尝试使用
string
作为您的
SelectedValue

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

这可能是问题中的一个输入错误-但是您的属性返回带大写字母T的_Test,并设置带小写字母T的_Test,这会给您带来这些症状,特别是当_Test是您在其他地方定义的实际变量时。您的项也是字符串,但测试的数据类型很短,这会导致问题。啊,抱歉!这是一种失败类型,在我的解决方案“返回测试”中。这不是问题所在。但是谢谢!您好@sa_ddam213和Daniel,我试图将其更改为“string”,但它仍然不起作用try
SelectedItem=“{Binding Test,Mode=TwoWay}”
这可能是问题中的一个输入错误-但是您的属性返回_Test和大写字母T,并设置_Test和小写字母T,这会给您带来这些症状,特别是如果_Test是您在其他地方定义的实际变量。您的项也是字符串,但是测试的数据类型很短,这会导致问题。啊,对不起!这是一种失败类型,在我的解决方案“返回测试”中。这不是问题所在。但是谢谢!您好@sa_ddam213和Daniel,我试图将其更改为“string”,但它仍然不工作try
SelectedItem=“{Binding Test,Mode=TwoWay}”
这是我选择它们时所做的:`string Test;if(cbTest.SelectedIndex==0){test=“Left”\u vm.test=test;}else if(cbTest.SelectedIndex==1){test=“Right”\u vm.test=test;}else if(cbTest.SelectedIndex==2){test“Center”;\u vm.Test=Test;}`这是我选择它们时所做的:`string Test;if(cbTest.SelectedIndex==0){Test=“Left”;\u vm.Test=Test;}else if(cbTest.SelectedIndex==1){Test=“Right“;_vm.Test=Test;}如果(cbTest.SelectedIndex==2){Test=“Center”;_vm.Test=Test;}`