“可点击”文本块?

发布于 2024-10-04 15:47:43 字数 223 浏览 3 评论 0原文

我有一个 WP7 应用程序,我希望有一个“可点击”的 TextBlock 区域,当用户单击 TextBlock 时,它会将其置于编辑模式(不同的控制)。 这将为用户在编辑文本之前添加另一个明确的步骤。

TextBlock 没有点击事件(这并不让我感到惊讶)。

有什么办法可以做到这一点吗?包裹在另一个控件或类似的东西中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

靑春怀旧 2024-10-11 15:47:43

是的,有一个点击事件。它称为 MouseLeftButtonDown

textBlock1.MouseLeftButtonDown +=new MouseButtonEventHandler(textBlock1_MouseLeftButtonDown);

private void textBlock1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}

Yes, there is a click event. Its called MouseLeftButtonDown

textBlock1.MouseLeftButtonDown +=new MouseButtonEventHandler(textBlock1_MouseLeftButtonDown);

private void textBlock1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}
等往事风中吹 2024-10-11 15:47:43

我通过在其模板内创建一个带有 TextBlock 的按钮解决了这个问题,如下所示:

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

它有更多的标记,但您不需要编写任何代码,它由框架提供并且可以正常工作。

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.

没有伤那来痛 2024-10-11 15:47:43

您可以使用手势支持关联单击事件。

编辑:通过这样做,您的行为将与用户在操作过程中在显示屏上移动手指时的单击(点击)一致。单击在鼠标按下、移开控制、鼠标松开时不会触发。点击手势的作用相同。并非所有用户都知道他们可以通过移动鼠标/手指来中止单击,但有些用户知道,如果处理不当,您将陷入困境。 MouseLeftButtonUp 处理的工作方式相同,但我不建议依赖于此,因为它不是设计使然,并且可能会出现意外的更改或与其他控件发生冲突。

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.

情深已缘浅 2024-10-11 15:47:43

有几个答案建议使用 MouseLeftButton 事件,但我看到很多这种方法出错的例子。
我建议将 TextBlock (或其他控件)放入具有自定义样式的按钮中(删除除内容控件之外的所有边框/边距)。好多了!

 <Button Style={StaticResource OnlyContentStyle}>
      <Button.Content>
           <TextBlock />
      </Button.Content>
 </Button>

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>
初相遇 2024-10-11 15:47:43

另一种方法是使用 Seva 的解决方案,但添加更多内容,这样滚动就不会触发“点击”

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 );
     }
}

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 );
     }
}
你怎么敢 2024-10-11 15:47:43

听起来你只需要一个按钮。更改控件模板以删除边框样式等,并创建您自己的(或清除现有的)视觉状态。或者甚至对其进行编辑,使“压抑”状态产生一点 3D 投影。

此外,通过按钮,如果您使用 MVVM,还可以使用按钮上的命令。

卢克

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

橙幽之幻 2024-10-11 15:47:43
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;
    }

我更喜欢添加一个事件并动态执行其余事件。

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.

ペ泪落弦音 2024-10-11 15:47:43

可以使用 PointerReleased 来捕获 UWP 控件的单击事件

Catch Click event for UWP Control can be done using PointerReleased

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文