我已经使用 Owner
属性创建了我的 UserControl
<shared:NavigationControl Owner="{Binding ElementName=This, Converter={StaticResource TestConverter}}" />
此
属性是我页面的x:Name
。
下面是代码:
public MvxWindowsPage Owner
{
get { return (MvxWindowsPage)GetValue(OwnerProperty); }
set { SetValue(OwnerProperty, value); }
}
public static readonly DependencyProperty OwnerProperty =
DependencyProperty.Register("Owner", typeof(MvxWindowsPage), typeof(NavigationControl), null);
我创建了 TestConverter
来检查绑定(bind)是否正常。
为什么 Owner
属性在 cs 中为 null
?
最佳答案
问题很可能出在您的 This
属性中。您必须确认它不为空。添加一个 PropertyChanged 回调,然后调试并确认发送到您的 OwnerProperty 的值不为空。
public MvxWindowsPage Owner
{
get { return (MvxWindowsPage)GetValue(OwnerProperty); }
set { SetValue(OwnerProperty, value); }
}
public static readonly DependencyProperty OwnerProperty =
DependencyProperty.Register("Owner", typeof(MvxWindowsPage), typeof(NavigationControl), new PropertyMetadata(PropChanged));
public static void PropChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var value = e.NewValue; //confirm this isn't null
}
关于c# - 依赖属性 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42399485/