다른 컨트롤에서 동적으로 만든 사용자 지정 컨트롤이 있는데 부모의 VisualState를 기반으로 VisualState를 변경하려고합니다. VisualState는 열거자를 허용하는 DependencyProperty
이고 컨트롤은 내부적으로 OnPropertyChange
이벤트 내부의 컨트롤을 사용하여 크기 및 내부 레이아웃을 변경합니다. 속성은 두 컨트롤 모두에서 동일하게 만들어집니다 (물론 유형을 제외하고).PropertyChanged 이벤트가 발생했지만 값이 변경되지 않았습니다.
public ControlSize VisualState
{
get { return (ControlSize)GetValue(VisualStateProperty); }
set
{
if (value != VisualState)
{
SetValue(VisualStateProperty, value);
}
}
}
public static readonly DependencyProperty VisualStateProperty = DependencyProperty.RegisterAttached(nameof(VisualState), typeof(ControlSize), typeof(CountersListControl), new PropertyMetadata(ControlSize.Large, OnVisualStateChanged));
부모 컨트롤을 동적으로 구성 요소를 할당하고 새로운 제어 VisualState에의 VisualState를 결합 :
CounterControl cc = new CounterControl();
cc.SetBinding(CounterControl.ValueProperty, new Binding() { Path = new PropertyPath(nameof(Counter.Amount)), Source = counter, Mode = BindingMode.TwoWay });
//cc.DataContext = this;//I tried with it, but it doesn't change a thing
cc.SetBinding(CounterControl.VisualStateProperty, new Binding() { Path = new PropertyPath(nameof(VisualState)), Source = this, Mode = BindingMode.OneWay });
Value
속성은 Counter.Amount에 대한 문제없이 결합 및 VisualState 너무 않는 것으로 보인다.
그러나 자식 값은 그렇지 않지만 부모가 변경되면 OnVisualState
메서드가 호출됩니다.
업데이트 :
Error: Converter failed to convert value of type 'Windows.Foundation.Int32' to type 'ControlSize';
ControlSize
가 열거, 그것을 변환 할 수 있어야하므로 : 나는 @EdPlunkett에 의해 제안 바인딩, 나는 다음과 같은 메시지를 얻고 있었다 디버깅.
이것은 DataContext와는 아무런 관련이 없습니다. 이것은 순진한 새로운 XAML 사람들에게 어디서나'DataContext = this'를 설정하도록하는 사람들의 이야기입니다. 더러운 광기. 개와 고양이. 세상에! –
오작동 바인딩에서 [PresentationTraceSources.SetTraceLevel()] (https://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources.settracelevel (v = vs.110) .aspx) 런타임에 디버그 추적에서 얻은 것을 확인하십시오. –
@EdPlunkett 알아 두어야 할 점은 솔루션을 검색하는 동안 발견 한 많은 질문에서 많은 의견이 언급했기 때문입니다. 나는 단지 그런 주석을 막고 싶었다. –