15

I am developing a WPF application, and have a TextBlock which I want to use command binding to trigger a command on when clicked. What's the best way to achieve this?

  • The TextBlock-control does not have a Command property, but it does have a CommandManager. What is this? Can it be used for command bindings? I've seen many other controls as well with this property..

  • Is there some control I have overseen that can be used? Is it e.g. recommended to use a button and style it to not look like a button?

  • Is there some controls supporting Command bindings which I can wrap around the TextBlock?

  • Should I create a custom control which basically is a TextBlock, but with extra properties Command and CommandArgument which enables command binding on e.g. the MouseLeftButtonDown property.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
stiank81
  • 25,418
  • 43
  • 131
  • 202

3 Answers3

17

Is there some control I have overseen that can be used? Is it e.g. recommended to use a button and style it to not look like a button?

Yes. The simplest approach would be to re-template a button to act like a TextBlock and leverage the command property on the button class.

Something like this:

<ControlTemplate TargetType="Button">
        <TextBlock Text="{TemplateBinding Content}" />
    </ControlTemplate>
...
<Button Content="Foo" Command="{Binding Bar}" />
Brad Cunningham
  • 6,402
  • 1
  • 32
  • 39
  • Thanks. Works like expected - and it's actually pretty neat..! I've read up on ControlTemplates in "WPF Unleased" now, and this feels right. What does the "Text="{TemplateBinding Content}" " do anyway? Bind what I set to the buttons Content property to the Text property on the TextBlock? – stiank81 Nov 25 '09 at 07:42
  • Yup, that is exactly what it does. – Brad Cunningham Dec 01 '09 at 21:48
4

The below XAML can be used to add a Command Binding to a WPF TextBlock which will then work on a mouse action.

<TextBlock FontWeight="Bold" Text="Header" Cursor="Hand">
    <TextBlock.InputBindings>
        <MouseBinding Command="ApplicationCommands.Cut" MouseAction="LeftClick"/>
    </TextBlock.InputBindings>
</TextBlock>

The Command can be one of the built-in application commands, which would use the syntax as shown above, or it can be a custom Command that inherits from the ICommand Interface. In that case, the syntax would be:

<MouseBinding Command="{Binding myCustomCommand}" MouseAction="LeftClick"/>

The MouseAction doesn't provide any Intellisense tips on what to put there (in VS2015), so you have to do a little digging to get the valid enumerations.

As of .NET 4.5, the valid entries for MouseAction are:

  • LeftClick - left mouse button click.
  • LeftDoubleClick - left mouse button double-click.
  • MiddleClick - middle mouse button click.
  • MiddleDoubleClick - middle mouse button double-click.
  • None - No action.
  • RightClick - right mouse button click.
  • RightDoubleClick - right mouse button double-click.
  • WheelClick - A mouse wheel rotation.

Constants shown above are taken from MSDN.

Stewbob
  • 16,759
  • 9
  • 63
  • 107
Praveen
  • 41
  • 5
  • While this code may answer the question, it would be better to explain _how_ it solves the problem without introducing others and _why_ to use it. Code-only answers are not useful in the long run. – Benjamin W. Feb 09 '16 at 18:35
  • Thanks for the suggestion @BenjaminW. This is my first answer in the community :-) – Praveen Feb 12 '16 at 18:11
  • @Praveen, good answer. It was exactly what I needed. I edited your answer to include more description and usage tips. – Stewbob Mar 25 '16 at 19:43
0
<Window.Resources>
<CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
</Window.Resources>


<TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
<TextBox.CommandBindings>
<StaticResource ResourceKey="binding"></StaticResource>
</TextBox.CommandBindings>
</TextBox>

Have you seen http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/BindTextBoxsavecommandtoCommandBinding.htm

MrWind
  • 9
  • 2