为 TextBlock 启用命令绑定

发布于 2024-08-12 10:04:06 字数 418 浏览 4 评论 0原文

我正在开发一个 WPF 应用程序,并且有一个 TextBlock,我想使用命令绑定在单击时触发命令。实现这一目标的最佳方法是什么?

  • TextBlock 控件没有 Command 属性,但有 CommandManager。这是什么?它可以用于命令绑定吗?我见过许多其他控件也具有此属性。

  • 是否有一些我监督过的可以使用的控件?例如,是否建议使用按钮并将其设计为看起来不像按钮?

  • 是否有一些控件支持命令绑定,我可以将它们包裹在 TextBlock 上?

  • 我是否应该创建一个自定义控件,它基本上是一个 TextBlock,但具有额外的属性 Command 和 CommandArgument,可以在例如 MouseLeftButtonDown 属性上启用命令绑定。

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.

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

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

发布评论

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

评论(3

得不到的就毁灭 2024-08-19 10:04:06

是否有一些我监督过的可以使用的控件?例如,是否建议使用按钮并将其设计为看起来不像按钮?

是的。最简单的方法是重新模板化按钮,使其像 TextBlock 一样工作,并利用按钮类上的命令属性。

像这样的东西:

<ControlTemplate TargetType="Button">
        <TextBlock Text="{TemplateBinding Content}" />
    </ControlTemplate>
...
<Button Content="Foo" Command="{Binding Bar}" />

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}" />
橙幽之幻 2024-08-19 10:04:06

下面的 XAML 可用于将命令绑定添加到 WPF TextBlock,然后该文本块将作用于鼠标操作。

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

Command 可以是内置应用程序命令之一,它将使用如上所示的语法,也可以是继承自 的自定义 Command ICommand 接口。在这种情况下,语法将是:

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

MouseAction 不提供任何有关放置内容的 Intellisense 提示(在 VS2015 中),因此您必须进行一些挖掘才能获得有效的枚举。

从 .NET 4.5 开始,MouseAction 的有效条目是:

  • LeftClick - 鼠标左键单击。
  • LeftDoubleClick - 双击鼠标左键。
  • MiddleClick - 单击鼠标中键。
  • MiddleDoubleClick - 双击鼠标中键。
  • 无 - 不采取任何行动。
  • RightClick - 单击鼠标右键。
  • RightDoubleClick - 双击鼠标右键。
  • WheelClick - 鼠标滚轮旋转。

上面显示的常量取自 MSDN

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.

却一份温柔 2024-08-19 10:04:06
<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>

您是否看过 http://www.java2s.com/Tutorial/ CSharp/0470__Windows-Presentation-Foundation/BindTextBoxsavecommandtoCommandBinding.htm

<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

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