当它被数据绑定时需要对Silverlight RadioButton采取行动

时间:2012-01-09 23:22:41

标签: silverlight

我正在创建一个具有一些附加功能的自定义RadioButton。我需要以某种方式订阅“OnDataBound”事件 - 有这样的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我能想到的最简单的方法:

1)创建一个UserControl,用RadioButton封装DependencyProperty,其中包含CustomIsChecked个对象:

/// <summary>
    /// Interaction logic for CustomRadioButton.xaml
    /// </summary>
    public partial class CustomRadioButton : UserControl
    {
        public CustomRadioButton()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region CustomIsChecked
        public bool CustomIsChecked
        {
            get { return (bool)GetValue(CustomDataContextProperty); }
            set { SetValue(CustomDataContextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CustomIsChecked.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CustomIsCheckedProperty =
            DependencyProperty.Register("CustomIsChecked", typeof(bool), typeof(CustomRadioButton), new UIPropertyMetadata(new PropertyChangedCallback((dpo, dpce) => 
            {
                MessageBox.Show("IsChecked has changed!");
            })));
        #endregion
    }

UserControl的XAML部分中,将IsChecked的{​​{1}}绑定到RadioButton

CustomIsChecked

瞧!