WPF组合框绑定到非字符串对象

WPF组合框绑定到非字符串对象,wpf,mvvm,combobox,binding,mvvm-light,Wpf,Mvvm,Combobox,Binding,Mvvm Light,我使用的是MVVM(MVVM Light Toolkit),在视图模型上有一个属性,它公开了一个对象列表。对象包含两个属性,两个字符串,它们与缩写和描述相关。我希望组合框将配对显示为“缩写-描述”。如果我使用数据模板,它很容易做到这一点 我在视图模型上有另一个属性,它表示应该显示为选中的对象——组合框中的选中项。我正在将ItemsSource绑定到列表,因为它代表了可用选择的范围,并且正在尝试将SelectedItem绑定到此对象。我拼命想弄明白为什么我不能让它工作,感觉自己越来越像一个骗局 在

我使用的是MVVM(MVVM Light Toolkit),在视图模型上有一个属性,它公开了一个对象列表。对象包含两个属性,两个字符串,它们与缩写和描述相关。我希望组合框将配对显示为“缩写-描述”。如果我使用数据模板,它很容易做到这一点

我在视图模型上有另一个属性,它表示应该显示为选中的对象——组合框中的选中项。我正在将ItemsSource绑定到列表,因为它代表了可用选择的范围,并且正在尝试将SelectedItem绑定到此对象。我拼命想弄明白为什么我不能让它工作,感觉自己越来越像一个骗局

在试图了解这一点的原因时,我创建了相同的方法,只使用一个字符串列表和一个选定的字符串。这很好用。所以,它显然和打字有关。。。也许是选择平等的原因?或者可能与数据模板有关

以下是XAML:

<Window x:Class="MvvmLight1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="300"
        Width="300"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <DataTemplate x:Key="DataTemplate1">
                <StackPanel Orientation="Horizontal">
                    <TextBlock TextWrapping="Wrap" Text="{Binding CourtCode}"/>
                    <TextBlock TextWrapping="Wrap" Text=" - "/>
                    <TextBlock TextWrapping="Wrap" Text="{Binding CourtDescription}"/>
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        <ComboBox x:Name="cmbAbbrevDescriptions" Height="35" Margin="25,75,25,25"  VerticalAlignment="Top" ItemsSource="{Binding Codes}" ItemTemplate="{DynamicResource DataTemplate1}" SelectedItem="{Binding selectedCode}" />
        <ComboBox x:Name="cmbStrings" Height="35" Margin="25" VerticalAlignment="Top" ItemsSource="{Binding strs}" SelectedItem="{Binding selectedStr}"/>
    </Grid>
</Window>

谢谢

您选择的代码Code3不在代码集合中。添加它以使选择按预期工作


您的selectedStr有效,因为它在集合中<代码>[strs.Add(“Hello”);]运行时不知道code2和code3应该相等


或者重写Court类中的equals方法,还记得重写GetHashCode方法。对,code3不在Codes集合中。但它相当于代码2。。。这就是我错过这条船的地方。我想说的是“在所有这些可能的类型中,选择一种与此匹配的类型”。@Oggy-如果我理解,你是说组合框试图设置所选项目,但它无法确定对象实例之间的相等性?太棒了!它与WPF、绑定等无关。它只是一个围绕类型相等的愚蠢错误。谢谢你!
using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using System.Collections.Generic;

namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public const string CodesPropertyName = "Codes";
        private List<Court> _codes = null;
        public List<Court> Codes
        {
            get
            {
                return _codes;
            }

            set
            {
                if (_codes == value)
                {
                    return;
                }

                var oldValue = _codes;
                _codes = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(CodesPropertyName, oldValue, value, true);
            }
        }

        public const string selectedCodePropertyName = "selectedCode";
        private Court _selectedCode = null;
        public Court selectedCode
        {
            get
            {
                return _selectedCode;
            }

            set
            {
                if (_selectedCode == value)
                {
                    return;
                }

                var oldValue = _selectedCode;
                _selectedCode = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(selectedCodePropertyName, oldValue, value, true);
            }
        }

        public const string strsPropertyName = "strs";
        private List<string> _strs = null;
        public List<string> strs
        {
            get
            {
                return _strs;
            }

            set
            {
                if (_strs == value)
                {
                    return;
                }

                var oldValue = _strs;
                _strs = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(strsPropertyName, oldValue, value, true);
            }
        }

        public const string selectedStrPropertyName = "selectedStr";
        private string _selectedStr = "";
        public string selectedStr
        {
            get
            {
                return _selectedStr;
            }

            set
            {
                if (_selectedStr == value)
                {
                    return;
                }

                var oldValue = _selectedStr;
                _selectedStr = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(selectedStrPropertyName, oldValue, value, true);
            }
        }


        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            Codes = new List<Court>();

            Court code1 = new Court();
            code1.CourtCode = "ABC";
            code1.CourtDescription = "A Court";

            Court code2 = new Court();
            code2.CourtCode = "DEF";
            code2.CourtDescription = "Second Court";

            Codes.Add(code1);
            Codes.Add(code2);


            Court code3 = new Court();
            code3.CourtCode = "DEF";
            code3.CourtDescription = "Second Court";
            selectedCode = code3;

            selectedStr = "Hello";
            strs = new List<string>();
            strs.Add("Goodbye");
            strs.Add("Hello");
            strs.Add("Ciao");

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvvmLight1.Model
{
    public class Court
    {
        public string CourtCode { get; set; }

        public string CourtDescription { get; set; }
    }
}
public override bool Equals(object o)
{
   var court = o as Court;
   if(court == null)
      return false;
   return CourtCode == court.CourtCode;
}