c# - 为什么我的 comboBox 的 SelectedValuePath 属性行为不正常?

标签 c# wpf data-binding

我正在努力寻找为什么我的 SelectedValuePath 没有导致我的组合框将 double 值传递到我的 View 模型属性 DelayLength。当我在执行期间更改组合框选择时,组合框变为红色并给出错误:

ConvertBack cannot convert value '[2 Seconds, 2]' (type 'KeyValuePair`2'). BindingExpression:Path=DelayLength; DataItem='TestViewModel' (HashCode=62605785); target element is 'ComboBox' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: DoubleConverter cannot convert from System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].

是否因为我相信我正确地遵循了文档而遗漏了一些简单的东西?

窗口.xaml

    <ComboBox Width="120" Height="24" HorizontalAlignment="Left" Margin="5,0"
              DisplayMemberPath="Key"
              SelectedValuePath="Value"
              ItemsSource="{Binding AvailableLengths}"
              SelectedItem="{Binding DelayLength}"/>

    <TextBox Text="{Binding DelayLength, Mode=OneWay}" IsReadOnly="True"></TextBox>

</StackPanel>

Window.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new TestViewModel();
    }
}

TestViewModel.cs

class TestViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    public Collection<KeyValuePair<string, double>> AvailableLengths
    {
        get
        {
            if (_availableLengths == null)
            {
                _availableLengths = new Collection<KeyValuePair<string, double>>()
                {
                    new KeyValuePair<string, double>("None", 0),
                    new KeyValuePair<string, double>("0.5 Seconds", 0.5),
                    new KeyValuePair<string, double>("1 Second", 1),
                    new KeyValuePair<string, double>("2 Seconds", 2),
                    new KeyValuePair<string, double>("3 Seconds", 3)
                };
            }

            return _availableLengths;
        }
    }

    private double _delayLength;

    public double DelayLength
    {
        get { return _delayLength; }
        set
        {
            _delayLength = value;
            RaisePropertyChanged(nameof(DelayLength));
        }
    }

    private Collection<KeyValuePair<string, double>> _availableLengths;
}

最佳答案

SelectedValuePath 不能与 SelectedItem 一起使用。您必须使用 SelectedValue

来自 MSDN 的自定义摘要:

The SelectedValuePath property provides a way to specify a SelectedValue for the SelectedItem. The SelectedItem represents an object in the Items collection and Control displays the value of a single property of the selected item. The SelectedValuePath property specifies the path to the property that is used to determine the value of the SelectedValue property.

关于c# - 为什么我的 comboBox 的 SelectedValuePath 属性行为不正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303728/

相关文章:

c# - 在 Masterpage 中定义派生类 (c#)

wpf - 操作 ObservableCollection 与替换列表

c# - 更改 numericUpDown 上的数字时数据绑定(bind)失败

c# - StringBuilder 中的 system.outofmemoryexception

c# - 如何在 4000 万词的列表中快速搜索?

c# - 拖放需要数学修复

带复选框的 wpf 组合框 - 所选项目

wpf - 拉伸(stretch)分离器不起作用

wpf - Windows 8 GUI,和 WPF/SilverLight/WinRT/Metro/BUILD 关系

data-binding - WinRT ImageSource 与 cookie 绑定(bind)