21

I have a WP7 app where I would like to have a "clickable" TextBlock area, that when a user clicks on the TextBlock, it puts it into edit mode (a different control). This would add another explicit step for a user before editing text.

There is no click event for the TextBlock (which doesn't surprise me).

Is there any way to do this? Wrapped in another control, or something similar?

pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
pearcewg
  • 9,545
  • 21
  • 79
  • 125

8 Answers8

23

Yes, there is a click event. Its called MouseLeftButtonDown

textBlock1.MouseLeftButtonDown +=new MouseButtonEventHandler(textBlock1_MouseLeftButtonDown);

private void textBlock1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
14

I have solved this by creating a Button with the TextBlock inside its Template like this:

<Button Click="button_Click">
    <Button.Template>
        <ControlTemplate>
            <TextBlock ... />
        </ControlTemplate>
    </Button.Template>
</Button>

It's a bit more markup, but you don't need to write any code, it is provided by the framework and works properly.

Mark Vincze
  • 7,737
  • 8
  • 42
  • 81
7

You can associate a click event using gesture support.

edit: by doing it this way your behaviour will be consistant with a Click (tap) with respect to the user moving their finger across the display during the operation. Click will not fire under mousedown, move off control, mouseup. Tap gesture works the same. Not all users know they can abort a click by moving their mouse/finger, but some do and you'll get caught out on this if not handling it correctly. MouseLeftButtonUp handling works the same way, but I wouldn't recommend relying on this as it's not by design and there may be unanticipated changes or conflicts with other controls.

Mick N
  • 14,892
  • 2
  • 35
  • 41
  • @Alan I disagree with the reasoning for your down vote. I disagree with your solution also. MouseLeftButton down is not behaviourly consistent with a click event in all cases. You argue keeping it simple, I would argue the marked answer is an approximation to the solution. I feel no cause to remove this answer as a result of your down vote. – Mick N Dec 08 '10 at 17:15
  • +1, no reason for a downvote here. Good tip about gesture support! – Fredrik Hedblad Jan 06 '11 at 08:20
5

There are several answers which advice to use MouseLeftButton events, but I saw a lot of examples where this approach goes wrong. I recommend to place TextBlock (or other control/controls) into a Button with a custom style (removed all borders/margins except content control). It's MUCH MUCH BETTER!

 <Button Style={StaticResource OnlyContentStyle}>
      <Button.Content>
           <TextBlock />
      </Button.Content>
 </Button>
Ku6opr
  • 8,126
  • 2
  • 24
  • 31
  • you need to create `OnlyContentStyle` style as mentioned in my answer – Ku6opr Jan 07 '13 at 10:01
  • and how do you do that? I just think that your idea is much better, but your answer is not very thourough. Also there are some quotes missing... – vidstige Jan 07 '13 at 17:41
  • @Ku6opr Please further explain how to create that style to be used by the button. – Migisha Aug 26 '13 at 08:35
  • 2
    In Blend: right click on Button, edit Template - Edit a Copy. Than remove all Borders, animations and so on – Ku6opr Nov 21 '13 at 18:24
3

Another way of doing it is using Seva's solution but adding a bit more so scrolling won't trigger the 'Click'

bool _IsMouseDown = false;

private void Textblock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _IsMouseDown = true;
}

private void Textblock_MouseLeave(object sender, MouseEventArgs e)
{
    _IsMouseDown = false;
}

private void Textblock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (_IsMouseDown)
    {
        _IsMouseDown = false;

        //ITS a CLick do something!
        Textblock clickedButton = (Textblock)sender;
        AddNumberToRolledNumbers(Convert.ToInt32(clickedButton.Tag),clickedButton.Background );
     }
}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
MasterCank
  • 31
  • 1
2

It sounds like you just need a button. Change the control template to remove the border styling etc. and make your own (or clear the existing) visual states. Or even edit it so the 'depressed' state gives a little 3D projection.

Also, with a button, you can also use the Command on the button if you're using MVVM.

Luke

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
0

Catch Click event for UWP Control can be done using PointerReleased

Hugo
  • 2,077
  • 2
  • 28
  • 34
0
private void txtPersonName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var txtPersonName = e.OriginalSource as TextBlock;

        if (txtPersonName == null) return;

        MouseButtonEventHandler eveMouseLeftButtonUp = (sen, eve) => NavigationService.Navigate(new Uri("/Views/PersonPage.xaml?personId=" + txtPersonName.Tag, UriKind.RelativeOrAbsolute));
        MouseEventHandler eveMouseMove = (sen, eve) => txtPersonName.MouseLeftButtonUp -= eveMouseLeftButtonUp;

        txtPersonName.MouseLeftButtonUp += eveMouseLeftButtonUp;
        txtPersonName.MouseMove += eveMouseMove;
    }

I prefer to add one event and do the rest dynamically.

Nic Bell
  • 513
  • 2
  • 7