【问题标题】:Dependency Property Not Binding to UserControl依赖属性未绑定到 UserControl
【发布时间】:2012-01-25 01:04:03
【问题描述】:

我有一个用户控件在绑定到 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();
    }

有什么想法吗?提前致谢

【问题讨论】:

    标签: c# wpf dependency-properties


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2011-07-29
      • 2014-08-31
      • 2017-12-24
      • 2017-12-18
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多