0

リストの再読み込みで選択したアイテムをバインドすることに関して、ComboBoxについて質問があります。

class Model : ViewModelBase
{
    /// <summary>
    /// The <see cref="Title" /> property's name.
    /// </summary>
    public const string TitlePropertyName = "Title";

    private string _title = "";

    /// <summary>
    /// Sets and gets the Title property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string Title
    {
        get
        {
            return _title;
        }

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

            RaisePropertyChanging(TitlePropertyName);
            _title = value;
            RaisePropertyChanged(TitlePropertyName);
        }
    }

    /// <summary>
    /// The <see cref="Id" /> property's name.
    /// </summary>
    public const string idPropertyName = "Id";

    private int _myId = 0;

    /// <summary>
    /// Sets and gets the id property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int Id
    {
        get
        {
            return _myId;
        }

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

            RaisePropertyChanging(idPropertyName);
            _myId = value;
            RaisePropertyChanged(idPropertyName);
        }
    }
}

バインドされたViewModel:

class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {

        PopulateCommand = new RelayCommand(() =>
        {
            Populate();
        });

        SetCommand = new RelayCommand(() =>
        {
            Id = 3;
        });
    }

    public void Populate()
    {
        MyList = new ObservableCollection<Model>(){
            new Model(){
                Id = 1,
                Title = "numer1"
            },
            new Model(){
                Id = 2,
                Title = "numer2"
            },
            new Model(){
                Id = 3,
                Title = "numer3"
            },
            new Model(){
                Id = 4,
                Title = "numer4"
            },
        };
    }

    public RelayCommand PopulateCommand { get; private set; }
    public RelayCommand SetCommand { get; private set; }

    /// <summary>
    /// The <see cref="MyList" /> property's name.
    /// </summary>
    public const string MyListPropertyName = "MyList";

    private ObservableCollection<Model> _myList = null;

    /// <summary>
    /// Sets and gets the MyList property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ObservableCollection<Model> MyList
    {
        get
        {
            return _myList;
        }

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

            RaisePropertyChanging(MyListPropertyName);
            _myList = value;
            RaisePropertyChanged(MyListPropertyName);
        }
    }

    /// <summary>
    /// The <see cref="Id" /> property's name.
    /// </summary>
    public const string IdPropertyName = "Id";

    private int _id = 0;

    /// <summary>
    /// Sets and gets the Id property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int Id
    {
        get
        {
            return _id;
        }

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

            RaisePropertyChanging(IdPropertyName);
            _id = value;
            RaisePropertyChanged(IdPropertyName);
        }
    }

}

xaml:

    <ComboBox
            ItemsSource="{Binding MyList}"
            DisplayMemberPath="Title" SelectedValue="{Binding Id, Mode=TwoWay}" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"/>
    <Button Content="Populate"
            Command="{Binding PopulateCommand}" />
    <Button Content="Set"
            Command="{Binding SetCommand}" />

    <TextBlock Text="{Binding Id}" Width="100"/>

したがって、populateButtonはコンボボックスバインディングのリストを作成し、setButtonはIDを3に設定します。次に、リストに再度データを入力すると、コンボボックスでIdが1になります。まだ3である必要がありますか?つまり、コンボボックスでid=3のモデルの選択項目を設定するべきではありません。

また、IsSynchronizedWithCurrentItemをtrueに設定せずに試しました。次に、リストをリロードした後、コンボボックスで何も選択されていません。selectedItemをIdと同期する方法はありますか?

リストを最初に初期化してから、IDを設定して、コンボボックスが選択したアイテムを更新できるようにする必要があるようです。しかし、最初にsetButtonを使用してからデータを入力すると、期待どおりの結果が得られるのはなぜですか(選択したアイテムは3番目のアイテムです)(最初に使用した場合のみ)?

4

1 に答える 1

4

リストを再作成した後、Id プロパティが変更されたことをビューに通知するだけです。

PopulateCommand = new RelayCommand(
    () =>
        {
            Populate();
            RaisePropertyChanged(() => Id);
        });

を使用する必要はありませんIsSynchronizedWithCurrentItem=true。これは、(たとえば) 2 つのリスト ボックスの同期を維持し、同じ選択項目を表示するために使用されます。

于 2013-03-16T17:46:52.793 に答える