How To Raise Property Changed events on a Dependency Property?

后端 未结 5 1875
长情又很酷
长情又很酷 2020-11-30 20:59

I have a control with two properties. One is a DependencyProperty, the other is an \"alias\" to the first one. How do I raise the PropertyChanged

5条回答
  •  广开言路
    2020-11-30 21:40

    I think the OP is asking the wrong question. The code below will show that it not necessary to manually raise the PropertyChanged EVENT from a dependency property to achieve the desired result. The way to do it is handle the PropertyChanged CALLBACK on the dependency property and set values for other dependency properties there. The following is a working example. In the code below, MyControl has two dependency properties - ActiveTabInt and ActiveTabString. When the user clicks the button on the host (MainWindow), ActiveTabString is modified. The PropertyChanged CALLBACK on the dependency property sets the value of ActiveTabInt. The PropertyChanged EVENT is not manually raised by MyControl.

    MainWindow.xaml.cs

    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            ActiveTabString = "zero";
        }
    
        private string _ActiveTabString;
        public string ActiveTabString
        {
            get { return _ActiveTabString; }
            set
            {
                if (_ActiveTabString != value)
                {
                    _ActiveTabString = value;
                    RaisePropertyChanged("ActiveTabString");
                }
            }
        }
    
        private int _ActiveTabInt;
        public int ActiveTabInt
        {
            get { return _ActiveTabInt; }
            set
            {
                if (_ActiveTabInt != value)
                {
                    _ActiveTabInt = value;
                    RaisePropertyChanged("ActiveTabInt");
                }
            }
        }
    
        #region INotifyPropertyChanged implementation
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ActiveTabString = (ActiveTabString == "zero") ? "one" : "zero";
        }
    
    }
    
    public class MyControl : Control
    {
        public static List Indexmap = new List(new string[] { "zero", "one" });
    
    
        public string ActiveTabString
        {
            get { return (string)GetValue(ActiveTabStringProperty); }
            set { SetValue(ActiveTabStringProperty, value); }
        }
    
        public static readonly DependencyProperty ActiveTabStringProperty = DependencyProperty.Register(
            "ActiveTabString",
            typeof(string),
            typeof(MyControl), new FrameworkPropertyMetadata(
                null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                ActiveTabStringChanged));
    
    
        public int ActiveTabInt
        {
            get { return (int)GetValue(ActiveTabIntProperty); }
            set { SetValue(ActiveTabIntProperty, value); }
        }
        public static readonly DependencyProperty ActiveTabIntProperty = DependencyProperty.Register(
            "ActiveTabInt",
            typeof(Int32),
            typeof(MyControl), new FrameworkPropertyMetadata(
                new Int32(),
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    
    
        static MyControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
    
        }
    
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    
    
        private static void ActiveTabStringChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            MyControl thiscontrol = sender as MyControl;
    
            if (Indexmap[thiscontrol.ActiveTabInt] != thiscontrol.ActiveTabString)
                thiscontrol.ActiveTabInt = Indexmap.IndexOf(e.NewValue.ToString());
    
        }
    }
    

    MainWindow.xaml

        
        
        
    
    

    App.xaml

    
    

提交回复
热议问题