Events DependencyProperties with Bools:False导致事件不激发

Events DependencyProperties with Bools:False导致事件不激发,events,xaml,user-controls,windows-runtime,Events,Xaml,User Controls,Windows Runtime,因此,我为我的xaml用户控件创建了一个bool依赖属性。但是,当该值在xaml中设置为false时,它不会在xaml中设置为true时触发事件。我怎样才能让它在任何情况下启动活动 public static readonly DependencyProperty AvailableProperty = DependencyProperty.Register("Available", typeof(bool), typeof(DetailPannel), new Property

因此,我为我的xaml用户控件创建了一个bool依赖属性。但是,当该值在xaml中设置为false时,它不会在xaml中设置为true时触发事件。我怎样才能让它在任何情况下启动活动

public static readonly DependencyProperty AvailableProperty =
    DependencyProperty.Register("Available", typeof(bool), typeof(DetailPannel),
    new PropertyMetadata(null, onAvailablePropertyChanged));

public bool Available
{
    get { return (bool)GetValue(AvailableProperty);  }
    set { SetValue(AvailableProperty, value); }
}

private async static void onAvailablePropertyChanged(DependencyObject d,   DependencyPropertyChangedEventArgs e)
{
    var obj = d as DetailPannel;
    bool avaible = (bool.Parse(e.NewValue.ToString())); 
    if(avaible == false )
    {            
        obj.PreviewImage.Source = await ConvertToGreyscale(obj.PreviewImage);
        obj.StateRelatedImage.Source = new BitmapImage(new Uri("ms-appx:///icon.png")); 
    }
} 

null
对于bool属性无效。更改PropertyMetadata以指定
false
true
作为默认值:

public static readonly DependencyProperty AvailableProperty =
    DependencyProperty.Register("Available", typeof(bool), typeof(DetailPannel),
    new PropertyMetadata(false, onAvailablePropertyChanged));
此外,PropertyChanged处理程序中的代码看起来可疑。不要使用
bool.Parse
,只需将
e.NewValue
转换为
bool

private async static void onAvailablePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var obj = d as DetailPannel;
    var available = (bool)e.NewValue; 

    if (!available)
    {
        ...
    }
}