Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle OnClick command of UserControl using command in Windows Phone 8
    text
    copied!<p>I want to handle some <code>Button.Click</code> events in a page that implements a <code>UserControl</code>.</p> <p>The <code>UserControl</code> looks like this:</p> <pre class="lang-xml prettyprint-override"><code>&lt;UserControl&gt; &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt; &lt;Button Command="{Binding ButtonClickedCommand, Source={RelativeSource Self}}" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>and the code behind:</p> <pre class="lang-cs prettyprint-override"><code>public partial class MapUserControl : UserControl { public MapTabBar() { InitializeComponent(); } public ICommand ButtonClickedCommand { get { return (ICommand)GetValue(ButtonClickedCommandProperty); } set { SetValue(ButtonClickedCommandProperty, value); } } public static readonly DependencyProperty ButtonClickedCommandProperty = DependencyProperty.Register( "ButtonClickedCommandProperty", typeof(ICommand), typeof(MapTabBar), new PropertyMetadata(null)); } </code></pre> <p>I use this <code>UserControl</code> in my <code>MainPage.xaml:</code></p> <pre class="lang-xml prettyprint-override"><code>&lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt; &lt;maps:Map x:Name="Map" ZoomLevel="7" /&gt; &lt;local:MapUserControl ButtonClickedCommand="{Binding ???}" /&gt; &lt;/Grid&gt; </code></pre> <p>How do I bind this <code>ButtonClickedCommand</code> to my function in the <code>MainPage.xaml.cs</code>:</p> <pre class="lang-cs prettyprint-override"><code> protected void Button_Clicked() { MessageBox.Show("button clicked"); } </code></pre> <p>Ultimately I want to manipulate the Map in my MainPage (center something, filter, reload). Is this the right approach – or is there another way?</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