3

モデルをDependencyPropertyカスタム コントロールとして使用することはできますか? 基本的に名前と描画用のデータポイントのリストを取得する画像ドロワーであるカスタムコントロールを作成したいので、これを行いたいです。

このようなもの:

モデル:

public class Draw : NotificationObject
{

public Draw(string name, List<System.Drawing.PointF> data)
    {
        Name = name;
        Data = data;

    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged(() => Name);
            }
        }
    }

    private List<System.Drawing.PointF> _data;
    public List<System.Drawing.PointF> Data
    {
        get { return _data; }
        set
        {
            if (_data != value)
            {
                _data = value;
                RaisePropertyChanged(() => Data);
            }
        }
    }
}
}

カスタム コントロール:

public class MyCanvas: System.Windows.Controls.Image
{
   static void itemsChangedCallBack(DependencyObject property,
   DependencyPropertyChangedEventArgs args)
    {
        MyCanvas searchTextBox = (MyCanvas)property;
        Console.WriteLine("got update");
        searchTextBox.Items = (Draw)args.NewValue;
    }

   public static readonly DependencyProperty ItemsProperty =
   DependencyProperty.Register("Items",
   typeof(Draw),
   typeof(MyCanvas),new PropertyMetadata(new PropertyChangedCallback(itemsChangedCallBack)));

    public Draw Items
    {
        get { return (Draw)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }
}

そしてもちろん XAML:

<myClass:MyCanvas x:Name="Canvas1" Items="{Binding drawModel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SizX="1600" SizY="200" />

そして ViewModel 呼び出し:

public class MainWindowViewModel : BaseViewModel
   {

   public Draw drawModel { get; set; }

   public MainWindowViewModel()
       {
           drawModel = new Draw("first", null); // custom control is notified
       }

   private someFunction() //within another thread but should not matter
       {
           drawModel.Data = newData; // custom control should be notified but is not
       }
}

私の問題は、ViewModel 内で drawModel.Data (プロパティ) を変更すると、カスタム コントロールに通知が届かないことです。モデルの代わりに単純な文字列に対して以前に行ったところ、うまくいきました。drawModel が初めて初期化されたときに機能しますが、後で Data プロパティを更新すると機能しません。

4

2 に答える 2

2

It works the first time the drawModel gets initialized but not if I update the Data property later on.

You should make Data an ObservableCollection<PointF> (or, even better, ObservableCollection<System.Windows.Point>, as WPF's Point already supports floating point values).

The issue is that adding to, removing from, or changing a List<T> doesn't provide any form of notication to WPF that things have changed. ObservableCollection<T> implements INotifyCollectionChanged, which is the collection version of INotifyPropertyChagned.

Note that this will still not trigger an update within your control. If you require that, you could subscribe to the Draw.Data's CollectionChanged event to be notified of changes wtihin the collection.

于 2013-10-23T22:13:35.713 に答える
0

あなたのビュー モデルは を実装していませんINotifiyPropertyChanged。コンストラクターでプロパティ drawModel を初めて設定すると、UI が完全に作成される前に呼び出され、UI が作成されると、読み込まれるときに現在の値が読み込まれます。ただし、値を再度変更する場合は、特定のプロパティの値が変更されたことを UI に通知する必要があります。

クラス Draw のいずれかのプロパティの値を変更した場合、それが実装されているため反映されている可能性がありINotifiyPropertyChangedますが、view-model にある drawModel の値を変更すると、これは INotifiyPropertyChanged を実装しないため、更新されません。

于 2013-10-23T15:37:49.533 に答える