I can not find a normal example on the use of Dependency property. So I registered the property in the helpers class.

private static readonly DependencyProperty FilterApplyProperty= DependencyProperty.Register("FilterApply", typeof(ICommand),typeof(EffectListSelection),new PropertyMetadata(1,OnValuePropertyChanged)); 

Here is the constructor

 public iCommand FilterValue { get { return (iCommand) GetValue(FilterApplyProperty); } set { SetValue(FilterApplyProperty, value);} } 

And the method itself

  private void OnClick(object sender, RoutedEventArgs e) { string listviewName = AssociatedObject.Name; temp = (e as ItemClickEventArgs).ClickedItem as FiltersViewModel; MainViewModel.Effect = temp.Type; this.EffectApply.Execute(this.temp.Type); <--- } 

Understood. Just needed to call a team and everything works.

    2 answers 2

    For starters, DependencyProperty needed for Binding , animation, styles, and the like to work. Usually this is needed for custom UI elements.

    Working with them is easy, in the same way as with ordinary properties. Plus, you can again put the value in styles, for example.


    My crystal ball says that you are trying to access an instance property or method from the OnValuePropertyChanged static method. This, of course, will not work. But you have an instance passed as parameter d , so maybe you want

     EffectListSelection self = (EffectListSelection)d; self.Command(); 

    If you are not actually writing a UI object, it may be worthwhile to use not DependencyProperty , but ordinary properties, implementing INotifyPropertyChanged .


    By the way, your DependencyProperty is not defined correctly. You did not give all the code, but even in the piece that you gave, the name of the property that you register ( "FilterApply" ) does not match the actual name of the property ( FilterValue ). Expect bugs and broken features. (Or, declare the DependencyProperty correctly.)

    • EffectListSelection effectListSelection = (EffectListSelection) d; effectListSelection.FilterApply = temp.Type; So I did, Now I need to execute a command that is in another class without creating an instance. Through Binding - SmiLe
    • @SmiLe: What is your “team”? If this is actually a method, then calling the method without an instance of the class will not work in principle, Binding will not help here. - VladD
    • public ICommand FilterApply {get {return _applyCommand ?? (_applyCommand = new CommandHandler (() => ApplyEffectAsync (File), _canExecute)); }} - SmiLe
    • @SmiLe: If there is no instance, then no, it will not work. - VladD

    Understood. Just needed to call a team and everything works.

      this.EffectApply.Execute(this.temp.Type);