0

スクリーンショット

これをかなり単純化しましたが、この問題に取り組む最善の方法について質問があります。

添付のスクリーンショットでは、主要連絡先は 1 つしか存在できないという要件があります。ユーザーは、リスト ビュー (スクリーン ショットの左側) または詳細ビュー (スクリーン ショットの右側) からプライマリ連絡先を変更できる必要があります。

つまり、ユーザーがリスト ビューで Jane Doe をチェックすると、John Smith の横にあるチェックボックスの選択が解除されます。Jane Doe のチェックボックスがオンの場合、詳細ビューでも同じことが起こり、John Smith の選択が解除されます。

MVVM フレームワークに Caliburn.Micro を使用しており、サンプル プロジェクトを添付しています。

私はプロジェクトを非常にシンプルにしようとしました。添付のプロジェクトの問題を解決できませんでした。解決方法についてさまざまなアイデアを得たいと思っています。

WpfApplication1.zip

ありがとう

4

2 に答える 2

1

IsPrimary1 つがプライマリになった場合に他のアイテムに何が起こるかを決定するのは、ビジネス オブジェクトの責任です。したがって、フラグを管理する何らかのオブジェクトが必要です。

ただし、それを行う場合(カスタムコンテナタイプ、メディエーターなど)、変更を仲介する何かが必要です

例えば

public class Mediator
{
    IList<Contact> _contacts = null;

    public Mediator(IList<Contact> contacts) 
    {
        _contacts = contacts;

        foreach(var c in contacts) 
        {
            c.PropertyChanged += ContactPropertyChanged;
        }
    }

    private bool _isChanging = false;

    private void ContactPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var current = sender as Contact;

        if(e.PropertyName == "IsPrimary" && !_isChanging && current.IsPrimary)
        {
            _isChanging = true;

            foreach(var c in _contacts.Where(x => x != current) 
            {
                c.IsPrimary = false;
            }

            _isChanging = false;
        }
    }
}

コンテナー コレクションを使用するなど、おそらくより良い方法があります (それ自体で propertychanged などをフックします... イベント ハンドラーにも注意してください!)。

オーバーロードを起動する、より一般的なテンプレート化されたバージョンを作成できます(したがって、サブクラス化してさまざまなメディエーターなどを簡単に作成できます)

public class Mediator<T>
{
    IList<T> _items = null;

    public Mediator(IList<T> items, params string[] watchedProperties) { ... etc

    protected virtual OnWatchedPropertyChanged(T sender, string PropertyName) 
    {
    }
}

public ContactMediator : Mediator<Contact>
{
     public ContactMediator(IList<Contact> contacts, params string[] watchedProperties) { ...

     override OnWatchedPropertyChanged(Contact object, string propertyName) { ... etc
}
于 2013-01-07T13:51:51.237 に答える
0

私が見たように、いくつかの基本的なオプションがあります。私が目指す方向は、Person という Model オブジェクトを作成し、IsPrimary というブール値のプロパティを持つことです。このモデル オブジェクトに INotifyPropertyChanged を実装させます。私の ViewModel オブジェクトには、Person オブジェクトの BindingList コレクション オブジェクトがあります。次に、チェックボックスにいくつかのコマンドを接続して、Person オブジェクトの変更をトリガーすると、それが実行されます。

于 2013-01-07T02:10:38.463 に答える