views:

478

answers:

1

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.

+3  A: 

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}" />
Foovanadil
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
Yup, that is exactly what it does.
Foovanadil