Manipulation events not firing - wpf

I'm new to developing in WPF for touchscreens, and I'm having trouble interpreting manipulation events. What I want to do is fairly simple I believe: when the user pinches anywhere on a UserControl, it will perform an action.
So, in the control I have (this is Surface 2.0 / Windows Touch):
XAML
<Grid Background="White" IsManipulationEnabled="True"
ManipulationStarting="Grid_ManipulationStarting"
ManipulationDelta="Grid_ManipulationDelta">
<!--Some content controls-->
</Grid>
C#
private void Grid_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = this;
}
private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//do the thing... you know, that thing you do
}
However, neither of these events fire when I rub my hands all over the screen. I think I must not understand the routing of the event in this situation. My monitor (3M MicroTouch PX) has not had any trouble understanding touch events or built-in manipulation like in ScatterViewItems.
EDIT: I removed controls from inside the Grid and they fire now, so I guess manipulations are being intercepted by the content. Sorry, should have been more clear about the contents of the control, because they're the problem it seems.
Specifically I think it has to do with the fact that I have a SurfaceListBox inside. I would imagine the SurfaceListBox is intercepting manipulation. Is there a way I can tell it to step off? I'm still trying to wrap my head around the way WPF does events.
Edit2: Going to paste some more complete code.
SEMANTICPANEL.XAML FULL
<UserControl x:Class="SemanticZoom.SemanticPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SemanticZoom"
xmlns:views="clr-namespace:SemanticZoom.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
</UserControl.Resources>
<Grid Background="White" IsManipulationEnabled="True" ManipulationStarting="Grid_ManipulationStarting" ManipulationDelta="Grid_ManipulationDelta">
<views:CategoryView x:Name="CategoryView"/>
<views:ShelfView x:Name="ShelfView" Visibility="Hidden" />
<views:BookView x:Name="BookView" Visibility="Hidden" />
</Grid>
SEMANTICPANEL.CS FULL
public partial class SemanticPanel : UserControl
{
public SemanticPanel()
{
InitializeComponent();
CategoryView.CategorySelected += new EventHandler(CategoryView_CategorySelected);
ShelfView.BookSelected += new EventHandler(ShelfView_BookSelected);
ShelfView.ZoomOut += new EventHandler(View_ZoomOut);
}
void View_ZoomOut(object sender, EventArgs e)
{
if (sender == ShelfView)
{
ShelfView.Visibility = System.Windows.Visibility.Hidden;
CategoryView.Visibility = System.Windows.Visibility.Visible;
}
else if (sender == BookView)
{
BookView.Visibility = System.Windows.Visibility.Hidden;
ShelfView.Visibility = System.Windows.Visibility.Visible;
}
}
void ShelfView_BookSelected(object sender, EventArgs e)
{
BookView.Books = ShelfView.BookList;
ShelfView.Visibility = System.Windows.Visibility.Hidden;
BookView.Visibility = System.Windows.Visibility.Visible;
}
void CategoryView_CategorySelected(object sender, EventArgs e)
{
ShelfView.Category = CategoryView.ActiveCategory;
ShelfView.Visibility = System.Windows.Visibility.Visible;
CategoryView.Visibility = System.Windows.Visibility.Hidden;
ShelfView.RefreshBooks();
}
private void Grid_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
//e.ManipulationContainer = this;
}
private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (e.DeltaManipulation.Scale.X < 0)
{
if (ShelfView.Visibility == System.Windows.Visibility.Visible)
{
ShelfView.Visibility = System.Windows.Visibility.Hidden;
CategoryView.Visibility = System.Windows.Visibility.Visible;
}
else if (BookView.Visibility == System.Windows.Visibility.Visible)
{
BookView.Visibility = System.Windows.Visibility.Hidden;
ShelfView.Visibility = System.Windows.Visibility.Visible;
}
}
}
CATEGORYVIEW.XAML
<UserControl x:Class="SemanticZoom.Views.CategoryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:s="http://schemas.microsoft.com/surface/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<!--CATEGORY TEMPLATE-->
<DataTemplate x:Name="CategoryTemplate" x:Key="CategoryTemplate">
<s:SurfaceButton Background="Gray" Click="CategoryClicked" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="200" Width="300">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Foreground="White" FontSize="20" FontWeight="Bold" />
</s:SurfaceButton>
</DataTemplate>
<!--CATEGORY STYLE-->
<Style TargetType="{x:Type s:SurfaceListBoxItem}">
<Setter Property="Width" Value="300"/>
<Setter Property="Height" Value="200"/>
</Style>
</UserControl.Resources>
<Grid>
<s:SurfaceListBox x:Name="CategoryList" ItemTemplate="{StaticResource CategoryTemplate}">
<s:SurfaceListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}"/>
</ItemsPanelTemplate>
</s:SurfaceListBox.ItemsPanel>
</s:SurfaceListBox>
</Grid>
Just don't judge me because a) yes I'm trying to emulate Semantic Zoom and b) yes I'm doing a hacky job of it. I just need a simple working concept. Each of the views is basically similar to CategoryView.

Thank you for your help Andriy, but it appears the problem was as simple as my monitor being far too insensitive. After I removed event hooks for all content on manipulation events, leaving only the hooks in the parent Grid, and then pressed VERY hard with two fingers on the screen, I got the events to fire. Probably need to mess a bit more with the routing, though.

Related

DragMove() will make the Border with cornerRadius lose its mouseover trigger state?

I have created a borderless window with rounded corners, and added the drag event and a trigger to it. Here is the simple code:
<Window x:Class="DebugTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DebugTest"
mc:Ignorable="d" Height="200" Width="200"
AllowsTransparency="True" WindowStyle="None" Background="Transparent">
<Border x:Name="MainBorder" CornerRadius="15" Background="White" BorderBrush="Black" BorderThickness="1">
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MainBorder,Path=IsMouseOver}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="5" Height="20" Width="20" Click="Button_Click"/>
</Grid>
</Border>
</Window>
public MainWindow()
{
InitializeComponent();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
But when I run the exe file, click on the blank area within the window, the button will appear very obvious flickering situation.
Strangely enough, this situation hardly occurs when debugging in Visual Studio instead of double click the file, and it also doesn't happen while CornerRadius="0".
It looks like it lost the mouseover trigger on click, but I can't think of any good way to avoid flicker appearing, and to satisfy the need for both with rounded corners, draggable, and with trigger.
Well, although I don't know why only rounded corners would cause DragMove() to trigger the MouseLeave event, I circumvented this with background code instead of using xaml trigger.
<Border x:Name="MainBorder" CornerRadius="15" Background="White"
BorderBrush="Black" BorderThickness="1"
MouseEnter="MainBorder_MouseEnter" MouseLeave="MainBorder_MouseLeave">
<Grid Visibility="Hidden" x:Name="TriggerBorder">
<Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="5" Height="20" Width="20" Click="Button_Click"/>
</Grid>
</Border>
bool dragMoving = false;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
dragMoving = true;
this.DragMove();
dragMoving = false;
}
private void MainBorder_MouseEnter(object sender, MouseEventArgs e)
{
TriggerBorder.Visibility = Visibility.Visible;
}
private void MainBorder_MouseLeave(object sender, MouseEventArgs e)
{
if (dragMoving) return;
TriggerBorder.Visibility = Visibility.Hidden;
}
Seems to work fine.

C# - Click event on multiple UIElement types collection

I'm displaying a collection of UI elements of different types (Rectangles and Images) in the canvas. Both derive from the UIElement type.
<ItemsControl ItemsSource="{Binding UiElementsCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Everything displays fine, but I want to have an event trigger to mouse events - when I click on/drag the specific element over the canvas I would like to receive this object (Rectangle or Image). I would like to do it the MVVM pattern. How can I do it?
You could implement an attached behaviour that hooks up the PreviewMouseLeftButtonDown of all UIElements in your source collection to a command of the view model:
public class MyBehavior
{
public static readonly DependencyProperty MouseLeftButtonDownCommandProperty
= DependencyProperty.RegisterAttached("MouseLeftButtonDownCommand",
typeof(ICommand), typeof(MyBehavior), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMouseLeftButtonDownCommandPropertyChanged)));
public static void SetMouseLeftButtonDownCommand(UIElement element, ICommand value)
{
element.SetValue(MouseLeftButtonDownCommandProperty, value);
}
public static ICommand GetMouseLeftButtonDownCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseLeftButtonDownCommandProperty);
}
private static void OnMouseLeftButtonDownCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
element.PreviewMouseLeftButtonDown += Element_MouseLeftButtonDown;
}
private static void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UIElement element = sender as UIElement;
ICommand command = GetMouseLeftButtonDownCommand(element);
if (command != null)
command.Execute(element);
}
}
View:
<Window x:Class="WpfApplication1.Window6"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Window6" Height="300" Width="300">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<ItemsControl ItemsSource="{Binding UiElementsCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="local:MyBehavior.MouseLeftButtonDownCommand"
Value="{Binding DataContext.TheCommand, RelativeSource={RelativeSource AncestorType=Window}}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
View Model:
public class ViewModel
{
public ObservableCollection<UIElement> UiElementsCollection { get; } = new ObservableCollection<UIElement>()
{
new Button { Content = "btn" },
new Border { Background = Brushes.Yellow, Width = 10, Height = 10 }
};
public ICommand TheCommand { get; } = new DelegateCommand<UIElement>(element =>
{
MessageBox.Show(element.GetType().ToString());
});
}
You will obviously need an implementation of the ICommand interface. The DelegateCommand class is available in Prism: https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommand.cs
Commands are pretty essential to an MVVM application though so you probably knew this already. You could refer to the following blog post for more information about how to handle events using commands in MVVM: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/

Empty textbox on mousclick in combination with language change in WPF

I have a textbox with a default text. When I focus the textbox it is cleared so I can write, and if I unfocus without writing anything the default text reappears.
I also have two radiobuttons for selecting the language. The languages are provided as xaml resourcefiles and the default text in the textbox is connected to that using DynamicResource.
My problem is that the language change only work as long as I haven't focused the textbox. If I focus the textbox and then unfocus it without changing anything, the textbox no longer changes language.
I'm guessing that is because once it's changed (cleared) it's no longer linked to the dynamic resource, because WPF considers my onfocus changes as user input, but I can't figure out how to get around that and make it change language even if I've clicked the textbox.
The second textbox don't have any focus behaviour and in that one the language change works as it should, i.e. it changes the language as long as I haven't actually written something.
MainWindow xaml:
<Window x:Class="Textbox_langauge_buggseek.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Textbox_langauge_buggseek"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="TextBox" HorizontalAlignment="Left" Height="46" Margin="84,55,0,0" TextWrapping="Wrap" Text="{DynamicResource ResourceKey=TB}" VerticalAlignment="Top" Width="334" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/>
<TextBox x:Name="TextBox_Copy" HorizontalAlignment="Left" Height="46" Margin="84,123,0,0" TextWrapping="Wrap" Text="{DynamicResource ResourceKey=TB}" VerticalAlignment="Top" Width="334"/>
<RadioButton x:Name="En" Content="En" GroupName="Lang" HorizontalAlignment="Left" Margin="391,216,0,0" VerticalAlignment="Top" Checked="En_Checked" IsChecked="True"/>
<RadioButton x:Name="Se" Content="Se" GroupName="Lang" HorizontalAlignment="Left" Margin="391,234,0,0" VerticalAlignment="Top" Checked="Se_Checked"/>
</Grid>
</Window>
MainWindows cs:
using System;
using System.Windows;
using System.Windows.Controls;
namespace Textbox_langauge_buggseek
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetLanguageDictionary();
}
//*****************************************************************************************
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox box = sender as TextBox;
box.Text = box.Text == (string)this.Resources["TB"] ? string.Empty : box.Text;
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox box = sender as TextBox;
box.Text = box.Text == string.Empty ? (string)this.Resources["TB"] : box.Text;
}
//*****************************************************************************************
private void En_Checked(object sender, RoutedEventArgs e)
{
SetLanguageDictionary("En");
}
private void Se_Checked(object sender, RoutedEventArgs e)
{
SetLanguageDictionary("Se");
}
//*****************************************************************************************
private void SetLanguageDictionary(string language = "En")
{
ResourceDictionary dict = new ResourceDictionary();
switch (language)
{
case "Se":
dict.Source = new Uri("..\\Resources\\Se.xaml", UriKind.Relative);
break;
default:
dict.Source = new Uri("..\\Resources\\En.xaml", UriKind.Relative);
break;
}
this.Resources.MergedDictionaries.Add(dict);
}
}
}
En language xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text in the TextBox!</system:String>
</ResourceDictionary>
Se language xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text i textrutan!</system:String>
</ResourceDictionary>
Yes, when you set TextBox.Text in codebehind, the text does not know anymore that it has to take a value from the Rersource. To avoid it you can change the text using pure XAML, with Triggers.
Remove TextBox's event handlers and add a style like this:
<TextBox x:Name="TextBox" HorizontalAlignment="Left" Height="46" Margin="84,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="334">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Text" Value="" />
</Trigger>
<Trigger Property="IsFocused" Value="false">
<Setter Property="Text" Value="{DynamicResource ResourceKey=TB}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>

How to create controls from code in a custom control?

In MainPage.xaml.cs (Silverlight Application) I can do something like this:
StackPanel myStackPanel = new StackPanel();
Button myButton = new Button();
myButton.Content = "Button";
myButton.Width = 200;
myButton.Height = 30;
Button myButton1 = new Button();
myButton1.Content = "Button 1";
myButton1.Width = 200;
myButton1.Height = 30;
myStackPanel.Children.Add(myButton);
myStackPanel.Children.Add(myButton1);
this.LayoutRoot.Children.Add(myStackPanel);
What is the equivalent of this code in a custom control when I'm trying to create these controls from the code?
Update:
My question is probably too confusing. I'l try better formulation.
So, I have
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DemoAddControlLib">
<Style TargetType="local:DemoControlShowtime">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DemoControlShowtime">
<Grid x:Name="LayoutRootControl">
<Button x:Name="Button1" Content="Hi" Width="150" Height="30"></Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
And code:
DemoControlShowtime.cs
[TemplatePart(Name = "Button1", Type=typeof(Button))]
public class DemoControlShowtime : Control
{
public DemoControlShowtime()
{
this.DefaultStyleKey = typeof(DemoControlShowtime);
}
// Events
public override void OnApplyTemplate()
{
Button1 = (Button)GetTemplateChild("Button1");
}
private Button button1;
private Button Button1
{
get { return button1; }
set
{
if (button1 != null)
{
Button1.Click -= new RoutedEventHandler(myButton_Click);
}
button1 = value;
button1.Click += new RoutedEventHandler(myButton_Click);
}
}
void myButton_Click(object sender, RoutedEventArgs e)
{
Button1.Content = "Hello Button";
}
}
If I click on Button1 the Content changes from "Hi" to "Hello Button". I want, when Button1 is clicked, to add StackPanel with two buttons as its Children into the Grid LayoutRootControl.
I know there is Visibility property and put it into the xaml would be easier but I'm curious how to do it from the code.
I hope this is much clearer than the question was before.
The code isn't really any different to what you have. The only variation is that the field LayoutRoot is not created for you.
However with this line of code:-
Grid LayoutRoot = GetTemplateChild("LayoutRootControl") as Grid;
The rest of your code would be identical (although you should test whether LayoutRoot is null first).
It appears to me that your are just wondering how to use a custom control in multiple places.
I've created a custom control (MyCustomControl) that has the StackPanel shown in your code, then used it multiple times on the MainPage.
MyCustomControl.xaml
<UserControl x:Class="SilverlightApplication2.MyCustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<Button Content="Button 1" Height="30" Width="200"/>
<Button Content="Button 2" Height="30" Width="200"/>
</StackPanel>
MyCustomControl.xaml.cs
public partial class MyCustomControl : UserControl
{
public MyCustomControl()
{
InitializeComponent();
}
}
Then I've used that custom control twice in the main view.
MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:SilverlightApplication2"
d:DesignHeight="300" d:DesignWidth="400">
<StackPanel>
<local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
MainPage.xaml.cs
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
Output

How to correctly pass some custom parameters to event handlers in WPF?

What could be a correct way to assign parameters to objects in WPF window (objects as simple as rectangles) and pass them to event handlers on mouse clicks?
Thank you.
Here's one way:
Put this in the Window:
public static readonly RoutedUICommand clickCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(Window1));
void ClickRectangle(object sender, ExecutedRoutedEventArgs e)
{
if (e.Parameter == null)
return;
MessageBox.Show("parameter = " + e.Parameter.ToString());
}
Then in the XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Margin="50" Fill="Blue">
<Rectangle.CommandBindings>
<CommandBinding
Command="my:Window1.clickCommand" Executed="ClickRectangle" />
</Rectangle.CommandBindings>
<Rectangle.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="my:Window1.clickCommand" CommandParameter="123" />
</Rectangle.InputBindings>
</Rectangle>
</Grid>
</Window>

Resources