依赖属性不绑定到UserControl

时间:2012-01-25 01:04:03

标签: c# wpf dependency-properties

我有一个用户控件,它遇到了与IsEnabled的依赖项属性绑定的问题。我也试过手动设置IsEnabled =" false"而且似乎也没有用。

以下是代码:

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

任何想法?在此先感谢

1 个答案:

答案 0 :(得分:1)

很难确定,因为您没有在XAML中显示绑定,但是,默认情况下,绑定将在DataContext中设置的任何内容中查找绑定属性。我怀疑这是问题...

如果这个假设是正确的,那么类似solution is presented over here ......