C# Windows Phone 8为依赖项属性设置默认属性
我正在为我的WP8应用程序创建一个控件。我正在尝试设置依赖项属性的值,而不考虑其默认值。下面是我的代码C# Windows Phone 8为依赖项属性设置默认属性,c#,wpf,xaml,windows-phone-8,dependency-properties,C#,Wpf,Xaml,Windows Phone 8,Dependency Properties,我正在为我的WP8应用程序创建一个控件。我正在尝试设置依赖项属性的值,而不考虑其默认值。下面是我的代码 public BitmapImage EnabledImage { get; set; } public BitmapImage DisabledImage { get; set; } public bool ControlEnabled { get { return (bool)GetValue(ControlEnabledProperty); }
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”时,它不会设置值。表示未在图像控件上设置禁用的图像
我希望此控件设置属性,而不考虑其默认值
我也参考了这篇文章,但解决方案不起作用:
您知道这里有什么问题吗。您可以将代码修改为此,然后尝试:
public static readonly DependencyProperty ControlEnabledProperty =
DependencyProperty.Register("ControlEnabled", typeof(bool?), typeof(ucControl), new PropertyMetadata(null, OnImageStatePropertyChanged));