如何绑定到一个自定义属性在Silverlight自定义控件 [英] How to bind to a custom property in a Silverlight Custom control

查看:107
本文介绍了如何绑定到一个自定义属性在Silverlight自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义的控制,除其他外,以下内容:

I've created a custom control with, amongst others, the following:

public partial class MyButton : UserControl
{
  public bool Enabled
  {
    get { return (bool)GetValue(EnabledProperty); }
    set { 
      SetValue(EnabledProperty, value); 
      SomeOtherStuff();
      }
    }
  }

  public static readonly DependencyProperty EnabledProperty =
     DependencyProperty.Register("Enabled", typeof(bool), typeof(MyButton), new PropertyMetadata(true));

  public static void SetEnabled(DependencyObject obj, bool value)
  {
    obj.SetValue(EnabledProperty, value);
  }
  public static bool GetEnabled(DependencyObject obj)
  {
    return (bool) obj.GetValue(EnabledProperty);
  }
}

在我的XAML中,我(尝试)使用绑定设置Enabled属性:

In my XAML, I (try to) use binding to set the Enabled property:

<MyButton x:Name="myButtom1" Enabled="{Binding CanEnableButton}"/>

我知道我的控制之间的绑定底层的数据模型是有效的工作,我可以绑定IsEnabled'(潜在用户控件土生土长的属性)和它的作品如预期。不过,我的Enabled属性永远不会通过上述绑定集。我已经把断点在我的属性集/让他们从来没有被打到的。

I know the bind between my control and the underlying data model is valid and working as I can bind 'IsEnabled' (a native property of the underlying UserControl) and it works as expected. However, my Enabled property is never set via the above binding. I've put breakpoints on my property set/get and they never get hit at all.

我只能成像我已经错过了与我的自定义控件结合的东西。任何人都可以看到什么?

I can only imaging I've missed something relating to binding in my custom control. Can anyone see what?

我试过在我的控制实现INotifyPropertyChanged的(并呼吁从我的启用二传PropertyChanged事件)...但是,这并没有解决它。

I've tried implementing INotifyPropertyChanged on my control (and calling the PropertyChanged event from my Enabled setter) ... but that didn't fix it.

[BTW:如果你想知道为什么?我无法截取改变基本控制IsEnabled状态,所以我决定在实施和使用我自己的一个启用/禁用属性的版本(我叫启用) - 一个在那里我可以插入我自己的code到属性setter]

[ BTW: In case you are wondering "Why?": I can't intercept changes to the IsEnabled state of the base control, so I decided to implement and use my own version of a Enable/disable property (which I called Enabled) - one where I could plug my own code into the property setter ]

推荐答案

首先删除的setEnabled GetEnabled 对,对于附加属性才有意义这是不是你在做什么。

First of all drop the SetEnabled and GetEnabled pair, these only make sense for an attached property which is not what you are doing.

现在的主要问题是,你是错误的假设,你的属性格式的get /集成员结合时被调用下,他们没有。

Now your main problem is that you are under the false assumption that the get/set members of your propery get called during binding, they don't.

您需要的是传递一个回调方法,在属性元数据,它是在这里,你拦截的变化和采取其他行动,像这样: -

What you need is to pass a call back method in the property meta data, it's here that you intercept changes and take other actions like so:-

    public bool IsEnabled
    {
        get { return (bool)GetValue(IsEnabledProperty); }
        set { SetValue(IsEnabledProperty, value); }
    }

    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.Register(
            "IsEnabled",
            typeof(bool),
            typeof(MyButton),
            new PropertyMetadata(true, OnIsEnabledPropertyChanged));

    private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyButton source = d as MyButton;
        source.SomeOtherStuff();

    }

    private void SomeOtherStuff()
    {
         // Your other stuff here
    }

通过这个地方,不管属性为如何改变 SomeOtherStuff 程序将被执行。

With this in place regardless of how the propery is changed the SomeOtherStuff procedure will execute.

这篇关于如何绑定到一个自定义属性在Silverlight自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆