4

私はMVVM(MVVM Light Toolkit)を使用しており、ビューモデルにオブジェクトのリストを公開するプロパティがあります。オブジェクトには、略語と説明に関連する 2 つのプロパティ (両方とも文字列) が含まれています。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; }
    }
}

ありがとう!

4

2 に答える 2

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;
}
于 2010-06-07T07:19:18.203 に答える
0

SelectedCode Code3 が Codes コレクションにありません。選択が期待どおりに機能するように追加します。

selectedStr はコレクション内にあるため機能します。[strs.Add("Hello");]

于 2010-06-07T06:06:48.147 に答える