私は独自の依存プロパティTarget.Height
を通常のプロパティSource.Height
にバインドしています。BindingOperations.SetBinding()
を使用しています。 Target.Height
プロパティを更新すると、Source.Height
プロパティが更新されます。しかし、依存関係プロパティの実際の値ではなく、依存関係プロパティのデフォルト値が使用されます。これは意図された動作ですか?WPFデータバインディング:BindingOperations.SetBinding()は依存関係プロパティの値を使用しません。
ありがとうございます。コードを使用します:
public class Source
{
private int m_height;
public int Height
{
get { return m_height; }
set { m_height = value; }
}
}
public class Target : DependencyObject
{
public static readonly DependencyProperty HeightProperty;
static Target()
{
Target.HeightProperty =
DependencyProperty.Register("Height", typeof(int), typeof(Target),
new PropertyMetadata(666)); //the default value
}
public int Height
{
get { return (int)GetValue(Target.HeightProperty); }
set { SetValue(Target.HeightProperty, value); }
}
}
Source source = new Source();
Target target = new Target();
target.Height = 100;
Binding heightBinding = new Binding("Height");
heightBinding.Source = source;
heightBinding.Mode = BindingMode.OneWayToSource;
BindingOperations.SetBinding(target, Target.HeightProperty, heightBinding);
//target.Height and source.Height is now 666 instead of 100 ....
これは間違いなく奇妙な動作です。私はLINQpadでそれを再現することができました。私は値が66ではなく100であると予想します。答えを見ることを楽しみにしています。 – NathanAW
ソースコードの貼り付けとデバッグの両方のプロパティは、コードの最後の行をステップオーバーした後に666 ...奇妙です... – stukselbax