나는 당신에게 나쁜 소식이 있습니다.
내부 DependencyObject.SetValue
새 값이 이전 값과 같은지 확인하는 검사가 있습니다. 따라서 A.B
에 바인딩되어 있고 A.B.C
을 변경하면 A.B
의 PropertyChanged 이벤트가 발생하므로 Binding
mechanizm이이 이벤트를 처리하고 DependencyObject.SetValue
을 호출합니다. 그러나 이전 및 신규 A.B
값의 동일성으로 인해 DP에는 변경 사항이 적용되지 않습니다.
올바른 DP 실행을 수행하려면 A.B의 새 인스턴스를 만들어야합니다.이 인스턴스는 큰 두통으로 끝납니다.
업데이트]
당신은 그것의 속성이 변경 될 때 변경되었음을 알림을 지원하여 Freezable 개체를 사용할 수 있습니다. DependencyObject는 Freezables와 올바르게 작동하므로 다음 예제는 필요한 작업을 수행합니다.
모델 클래스 :
public class A
{
public A()
{
this.B = new B();
}
public B B
{
get; private set;
}
}
public class B : Freezable, INotifyPropertyChanged
{
protected override Freezable CreateInstanceCore()
{
return new B();
}
private string _c = "initial string";
public string C
{
get
{
return _c;
}
set
{
this._c = value;
this.OnPropertyChanged("C");
this.OnChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
var safe = this.PropertyChanged;
if (safe != null)
{
safe(this, new PropertyChangedEventArgs(name));
}
}
}
XAML :
<StackPanel>
<TextBox Text="{Binding A.B.C}" />
<TextBox Text="{Binding MyProperty.C}" />
<Button Click="Button_Click"/>
</StackPanel>
코드 숨김
public partial class TextBoxesView : UserControl
{
public TextBoxesView()
{
InitializeComponent();
this.A = new A();
this.DataContext = this;
BindingOperations.SetBinding(this, TextBoxesView.MyPropertyProperty, new Binding("A.B"));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.A.B.C = DateTime.Now.ToString();
}
public A A
{
get;
private set;
}
public B MyProperty
{
get
{
return (B)this.GetValue(TextBoxesView.MyPropertyProperty);
}
set
{
this.SetValue(TextBoxesView.MyPropertyProperty, value);
}
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty",
typeof(B),
typeof(TextBoxesView),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, (d, e) => { }));
}
당신은 바인딩에'모드 = TwoWay'를 추가해야 할 수도 있습니다. 그러나 나는 당신의 질문을 정확하게 이해하고 있는지 확신 할 수 없다. 코드를 추가 할 수 있을까? – Damascus
클래스 구조를 조금 만들고 여기에 코드를 붙여 넣으면 훨씬 나아질 것입니다 ... –