ToggleSwitch 및 TextBlock이 포함 된 ToggleSwitchItem 사용자 정의 컨트롤을 만들고 있습니다. 개인 ToggleSwitch 자식의 IsChecked 속성을 노출하는 데 사용하려는 IsChecked라는 종속성 속성을 정의했습니다.자식 중 하나에서 값을 버블 링하는 사용자 정의 컨트롤에서 종속성 속성을 정의하는 방법은 무엇입니까?
그러나 데이터 바인딩이 작동하지 않습니다 ...로드 될 때 기본값으로 유지됩니다.
무엇이 누락 되었습니까?
코드 : 데이터 바인딩에 대한
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register(
"IsChecked",
typeof(bool),
typeof(ToggleSwitchItem),
new PropertyMetadata(new PropertyChangedCallback
(OnIsCheckedChanged)));
public bool IsChecked
{
get
{
return (bool)GetValue(IsCheckedProperty);
}
set
{
SetValue(IsCheckedProperty, value);
}
}
private static void OnIsCheckedChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
ToggleSwitchItem item = (ToggleSwitchItem)d;
bool newValue = (bool)e.NewValue;
item.m_switch.IsChecked = newValue;
}
, 나는 다음에 사용하고 있습니다 : 일반 바닐라에 바인딩 할 때이 문제없이 작동으로
<phone:PhoneApplicationPage.Resources>
<myApp:SharedPreferences x:Key="appSettings"/>
</phone:PhoneApplicationPage.Resources>
IsChecked="{Binding Source={StaticResource appSettings},
Path=SomeProperty, Mode=TwoWay}"
된 SharedPreferences 클래스는, 잘 작동 위와 동일하게 스위치의 IsChecked 속성을 토글합니다.
감사합니다.
솔루션 (안토니의 도움으로) :
내가 지금처럼 사용자 정의 컨트롤의 생성자에서 내 사용자 컨트롤에 내 아이 토글 스위치를 결합 :
Binding binding = new Binding();
binding.Source = this;
binding.Path = new PropertyPath("IsChecked");
binding.Mode = BindingMode.TwoWay;
m_switch.SetBinding(ToggleSwitch.IsCheckedProperty, binding);
그리고 나는 제거 더 이상 필요하지 않은 콜백 :
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register(
"IsChecked",
typeof(bool),
typeof(ToggleSwitchItem),
null);
public bool IsChecked
{
get
{
return (bool)GetValue(IsCheckedProperty);
}
set
{
SetValue(IsCheckedProperty, value);
}
}
감사합니다 도움이 될 수. 순수한 XAML에서 바인딩을 사용하면 문제가 발생했습니다. 뒤에 코드에서 바인딩을 설정할 때만 작동합니다. – t9mike