0

コレクションである依存関係プロパティを持つユーザーコントロールを自分で作成しました。

    private static readonly DependencyPropertyKey VerticalLinesPropertyKey = DependencyProperty.RegisterReadOnly("VerticalLines", typeof(VerticalLineCollection), typeof(DailyChart), new FrameworkPropertyMetadata(new VerticalLineCollection()));
    public static DependencyProperty VerticalLinesProperty = VerticalLinesPropertyKey.DependencyProperty;


    public VerticalLineCollection VerticalLines
    {
        get
        {
            return (VerticalLineCollection)base.GetValue(VerticalLinesProperty);
        }
        set
        {
            base.SetValue(VerticalLinesProperty, value);
        }
    }

Window が次のようなコードでコントロールを使用していたときに、このコレクションを XAML から直接入力していました。

<chart:DailyChart.VerticalLines>
    <VerticalLine ... ... ... />
</chart:DailyChart.VerticalLines>

ここで、この固定された初期化を XAML から削除し、コレクションを ViewModel のプロパティにバインドしたいのですが、次のエラーが発生します。

Error   1   'VerticalLines' property cannot be data-bound.
Parameter name: dp

何か案は?

4

2 に答える 2

2

XAMLの例では、パーサーはVerticalLineCollection型が実装されていることを認識しているIListため、指定されたそれぞれに対してオブジェクトVerticalLineを作成し、コレクション自体VerticalLineを呼び出します。Add

ただし、コレクションをバインドしようとすると、セマンティクスは "VerticalLinesプロパティに新しいコレクションを割り当てる" になり、これは読み取り専用の依存関係プロパティであるため実行できません。プロパティのセッターは実際には非公開としてマークする必要があり、そうすることで代わりにコンパイル時エラーが発生します。

お役に立てれば!

于 2011-12-11T19:02:01.713 に答える
0

これは(True read-only dependency property)が原因だと思います。

読み取り専用のプロパティがあるため、次のように変更できます

        private static readonly DependencyPropertyKey VerticalLinesPropertyKey = DependencyProperty.Register("VerticalLines", typeof(VerticalLineCollection), typeof(DailyChart), new FrameworkPropertyMetadata(new VerticalLineCollection()));
        public static DependencyProperty VerticalLinesProperty = VerticalLinesPropertyKey.DependencyProperty;

リフレクターは答えを与えます:

    internal static BindingExpression CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent)
    {
        FrameworkPropertyMetadata fwMetaData = dp.GetMetadata(d.DependencyObjectType) as FrameworkPropertyMetadata;
        if (((fwMetaData != null) && !fwMetaData.IsDataBindingAllowed) || dp.ReadOnly)
        {
            throw new ArgumentException(System.Windows.SR.Get(System.Windows.SRID.PropertyNotBindable, new object[] { dp.Name }), "dp");
        }
        ....

これがうまくいくことを願っています

于 2011-12-11T19:15:23.570 に答える