1

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

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

Combobox 中填充了以下项目:

        for (int i = 1; i < 6; i++)
            cbTest.Items.Add(i);

我看到组合框中的项目,但它没有显示我之前选择的 SelectedValue。这是属性:

private short _testSpeed;
public short TestSpeed
{
    get
    {
        return _testSpeed;
    }
    set
    {
        _testSpeed= value;
        NotifyPropertyChanged();
    }
}

这是我在 SelectedChanged 上更改项目的时候

 _vm.TestSpeed = (short)Convert.ToInt16(cbTest.SelectedValue);

TestSpeed 在调试中给了我正确的数据,但是 selectedValue 绑定不起作用!?

4

1 回答 1

0

在您的情况下,绑定模式必须是OneWayToSource这样的SelectedValue="{Binding Path=TestSpeed, Mode=OneWayToSource}。当 TestSpeed 是stringint那是工作,当short- 不工作。我认为您需要编写特定的转换器来使用short或使用int,不要担心

于 2013-03-09T16:47:11.023 回答