Note that there are some explanatory texts on larger screens.

plurals
  1. POAttached behaviour on parent mvvm doesn't work
    text
    copied!<p>I have 2 Usercontrols, parent and child, child control has button witch I want to click with parent viewmodel method, but it doesn't work, please tell me what I miss in parent view, I have something like this:</p> <pre><code>XAML ... &lt;view:childUC vm:ChildBehaviuor.AddCommand="{Binding ExampleCommand}"/&gt; </code></pre> <p>Behavior code:</p> <pre><code> public static readonly DependencyProperty AddCommandProperty =DependencyProperty.RegisterAttached ( "AddCommand", typeof(ICommand), typeof(childBehavior), new PropertyMetadata(OnAddCommand) ); public static ICommand GetAddCommand(DependencyObject obj) { return (ICommand)obj.GetValue(AddCommandProperty); } public static void SetAddCommand(DependencyObject obj, ICommand value) { obj.SetValue(AddCommandProperty,value); } private static ICommand command; private static void OnAddCommand(DependencyObject sender, DependencyPropertyChangedEventArgs e) { child gp = sender as child; childBehavior.command = (ICommand)sender.GetValue(childBehavior.AddCommandProperty); if(gp != null &amp;&amp; command != null) { if ((e.NewValue != null) &amp;&amp; (e.OldValue == null)) { gp.AddButton.Click += ButtonClick; } else if ((e.NewValue == null) &amp;&amp; (e.OldValue != null)) { gp.AddButton.Click -= ButtonClick; } } } public static void ButtonClick(object sender,RoutedEventArgs eventArgs) { childBehavior.command.Execute(null); } </code></pre> <p>VM parent command:</p> <pre><code> public ICommand ExampleCommand { get { if (this.exampleCommand == null) { this.exampleCommand = new DelegateCommand(...); } return this.exampleCommand ; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload