これは質問の続きです:Trying to setup a custom DependencyObject. Clearly missing somethingです。元の質問を編集することは現実的ではありません。変更は大きすぎます。だから私は新鮮な質問を始めています。カスタムDependencyObjectsのバインディングの設定
私はUWPアプリケーションでカスタムDependencyObjects間のバインドを設定しようとしています。関連するコードは以下の通りです。私はActualWidthPropertyChangedへの呼び出しを見ていますが、WidthPropertyChangedへの呼び出しをトリガーしていません。私は何が欠けていますか?
class WindowsElement: DependencyObject
{
public WindowsElement()
{
}
public double Width
{
get
{
return (double)GetValue(WidthProperty);
}
set
{
SetValue(WidthProperty, value);
}
}
private static void WidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("WPC", element, o, width);
element.Width = width;
}
private static void ActualWidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("AWPC", o, e, width, element.Width);
element.ActualWidth = width;
}
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
"Width",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, WidthPropertyChanged));
public double ActualWidth {
get
{
return (double)GetValue(ActualWidthProperty);
}
set
{
SetValue(ActualWidthProperty, value);
}
}
public static readonly DependencyProperty ActualWidthProperty =
DependencyProperty.Register(
"ActualWidth",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, ActualWidthPropertyChanged));
public static void MessWithBindings()
{
WindowsElement we1 = new WindowsElement();
WindowsElement we2 = new WindowsElement();
var b = new Binding
{
Source = we2,
Path = new PropertyPath("ActualWidth")
};
BindingOperations.SetBinding(we1, WindowsElement.WidthProperty, b);
we2.ActualWidth = 13;
CommonDebug.LogLine(we1, we1.Width, we1.ActualWidth, we2, we2.Width, we2.ActualWidth);
}
}
https://msdn.microsoft.com/enラッパーの実装**:*例外的な状況を除いて、ラッパーの実装はGetValueおよびSetValue操作のみを実行する必要があります。それ以外の場合は、プロパティがXAMLで設定されている場合とコードで設定されている場合の動作が異なります。効率を上げるために、XAMLパーサは依存関係プロパティを設定するときにラッパーをバイパスします。 SetValue *を介してバッキングストアに話しかけます。 – Clemens
PropertyChangedイベントを発生させるためには(必要な場合)、PropertyChangedCallbackで行う必要があります。 – Clemens
@Clemensあなたは正しいです。ラッパーでPropertyChangedを呼び出すのは良い方法ではありませんでした。私は自分の返信を更新しました。この問題は特殊なケースになるはずです。 INotifyPropertyChangedインタフェースを実装する必要があります。 –