简体   繁体   中英

Windows Phone 8 set default property for dependency property

I was creating a control for my WP8 app. I am trying to set value for dependency property irrespective of its default value. Below is my code

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;
    }

this how i am calling it in 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"/>

it does not set the value when I set ControlEnabled = "False" . Means disabled image is not set on image control.

I want this control to set property irrespective of it default value.

I refer this post as well but solution not working : Windows Phone 8, use DependencyProperty for a usercontrol, PropertyChangedCallback and CoerceValueCallback issue

Any Ideas what wrong here.

you can modify your code to this, and try it:

public static readonly DependencyProperty ControlEnabledProperty =
    DependencyProperty.Register("ControlEnabled", typeof(bool?), typeof(ucControl), new PropertyMetadata(null, OnImageStatePropertyChanged));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM