[英]Windows Phone 8 set default property for dependency property
我正在为WP8应用程序创建控件。 我正在尝试为依赖项属性设置值,无论其默认值如何。 下面是我的代码
public BitmapImage EnabledImage { get; set; }
public BitmapImage DisabledImage { get; set; }
public bool ControlEnabled
{
get { return (bool)GetValue(ControlEnabledProperty); }
set { SetValue(ControlEnabledProperty, value); }
}
public static readonly DependencyProperty ControlEnabledProperty =
DependencyProperty.Register("ControlEnabled", typeof(bool), typeof(ucControl), new PropertyMetadata(OnImageStatePropertyChanged));
private static void OnImageStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (d as ucControl);
control.ControlEnabled = Convert.ToBoolean(e.NewValue);
control.OnImageStateChanged(e.NewValue);
}
private void OnImageStateChanged(object newValue)
{
if (Convert.ToBoolean(newValue) == true)
imgControl.Source = EnabledImage;
else
imgControl.Source = DisabledImage;
}
这就是我在xaml中称呼它的方式
<WP8:ucControl Grid.Row="0" Grid.Column="0" Height="92" Width="92" EnabledImage="/Images/img_on.png" DisabledImage="/Images/img_off.png" ControlEnabled="True"/>
当我设置ControlEnabled =“ False”时,它不会设置该值。 表示未在图像控件上设置禁用的图像。
我希望此控件设置属性,而不考虑其默认值。
我也引用此帖子,但解决方案不起作用: Windows Phone 8,对用户控件使用DependencyProperty,PropertyChangedCallback和CoerceValueCallback问题
任何想法这里有什么问题。
您可以对此进行修改,然后尝试:
public static readonly DependencyProperty ControlEnabledProperty =
DependencyProperty.Register("ControlEnabled", typeof(bool?), typeof(ucControl), new PropertyMetadata(null, OnImageStatePropertyChanged));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.