依赖属性未绑定到 UserControl

发布于 2024-12-28 13:58:51 字数 1184 浏览 2 评论 0原文

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

有什么想法吗?提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2025-01-04 13:58:51

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

如果这个假设是正确的,这里将提供类似的解决方案...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文