WPF ComboBox绑定到非字符串对象

时间:2010-06-07 01:34:30

标签: wpf mvvm combobox binding mvvm-light

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

我在视图模型上有另一个属性,它表示应该显示为选中的对象 - ComboBox中的选定项。我将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>

并且,如果有用,这里是ViewModel:

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; }
    }
}

谢谢!

2 个答案:

答案 0 :(得分:1)

运行时不知道code2和code3应该相等。

请参阅http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx

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

答案 1 :(得分:0)

您的SelectedCode Code3不在Codes集合中。添加它以便选择按预期工作。

您的selectedStr有效,因为它位于集合中。 [strs.Add("Hello");]