I have a RoutedUICommand that can be bind to multiple controls in x开发者_如何学JAVAaml. I have a few Button controls and an image which should be bound to this Command. It's very simple to bind a command with buttons like
<Button Name="btnAction1" Command="local:MainWindow.Command1"/>
But I am unable to find a way to bind a mousedown event of image to this command. Is there anything I am missing or there really has no way to do this.
check out MouseBinding http://msdn.microsoft.com/en-us/library/system.windows.input.mousebinding.aspx
Since the Image class doesn't define a Command property, you have a few options. You could write an attached behavior that will handle a MouseDown and raise a command.
Or you can do it all in XAML by creating a new Button style. As the Button can host anything, you can create a blank button style and it will look like an image:
<Style x:Key="EmptyButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then, when you use it, you would do:
<Button Name="btnAction1"
Style="{StaticResource EmptyBUtton}"
Command="local:MainWindow.Command1">
<Image Source="MySourceImage.jpg" />
</Button>
You could use an attached behavior, as shown in this article
精彩评论