Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your user control, you're setting the <code>CommandBinding</code> like this:</p> <pre><code>CommandTarget="{Binding ElementName=userControl11}" </code></pre> <p>If you look in the Output window while your program's running, you'll see this:</p> <pre><code>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=userControl11'. BindingExpression:(no path); DataItem=null; target element is 'Button' (Name=''); target property is 'CommandTarget' (type 'IInputElement') </code></pre> <p>This is because there's no element named <code>userControl11</code> in the current namescope. See <a href="http://msdn.microsoft.com/en-us/library/ms746659.aspx" rel="nofollow">this</a> for full details on how XAML namescopes work, but in short, XAML names are only visible within the page, window, or user control that they're declared in. You can't reference a name declared in your window from inside a user control.</p> <p>If you want to be able to set a command target on a user control via binding, it needs to be exposed as a dependency property. You'd add a declaration like this to the control:</p> <pre><code>public IInputElement CommandTarget { get { return (IInputElement)GetValue(CommandTargetProperty); } set { SetValue(CommandTargetProperty, value); } } public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(UserControl1), new UIPropertyMetadata(null)); </code></pre> <p>and in the user control's XAML, bind to this property:</p> <pre><code>&lt;Button Content="Click" Command="local:Commands.ClickCommand" CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=CommandTarget}" /&gt; </code></pre> <p>I can't actually get this to work in your project, but that's because I can't figure your project out. You seem to have two or three user controls named <code>UserControl1</code> in different namespaces, and the names of the files in the project don't correspond to their contents, and there's not very much in the way of useful commentary. (Just in general, instead of saying "this doesn't work," describe the behavior you're expecting; that makes it a lot clearer to someone trying to help you what your actual problem might be.)</p> <p>I hope this is helpful anyway.</p>
 

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