简体   繁体   中英

Dependency Property Not Binding to UserControl

I have a user control that is having issues binding to a dependency property for IsEnabled. I have also tried manually setting the IsEnabled="false" and that also appears to be not working.

Here is the code:

public partial class News : UserControl
{
    public static readonly DependencyProperty IsAuthenticatedProperty =
    DependencyProperty.Register(
    "IsAuthenticated",
    typeof(bool),
    typeof(News),
    new FrameworkPropertyMetadata(
    new PropertyChangedCallback(ChangeAuth)));

    public bool IsAuthenticated
    {
        get
        {
            return (bool) GetValue(IsAuthenticatedProperty);
        }
        set
        {
            SetValue(IsAuthenticatedProperty, value);
        }
    }

    private static void ChangeAuth(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is bool == false)
        {
            (source as News).UpdateAuth(false);
        }
        else 
        { 
           (source as News).UpdateAuth(true);
        }
    }

    private void UpdateAuth(bool value)
    {
        IsAuthenticated = value;
    }


    public News()
    {
        IsAuthenticated = false;
        this.IsEnabled = false;
        InitializeComponent();
    }

Any Ideas ? Thanks In Advance

It's hard to be certain since you haven't shown your binding in XAML, however, your binding will by default be looking for the bound property on whatever is set in the DataContext. I suspect this is the problem...

If this assumption is correct, a similar solution is presented over here ...

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