WPF学习系列032: 3.4.3 带有内建命令绑定的控件

本文介绍了WPF中TextBox控件的内置命令绑定功能,通过示例展示了如何使用Cut、Copy、Paste、Undo和Redo等命令与按钮进行绑定,实现高效便捷的文本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  •  
    1. WPF中的一些控件确有自己的命令绑定。最简单的例子就是TextBox件,它有自己的Cut CopyPaste命令的内建绑定,这些命令可以与剪贴板交互,还有UndoRedo命令的内建绑定。
    2. 下面的XAML展示了这些内建命令绑定的力量:
  • 3.4.3  带有内建命令绑定的控件

    <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Orientation="Horizontal"

    Height="25">

    <Button Command="Cut"

    CommandTarget="{Binding ElementName=textBox}"

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />

    <Button Command="Copy"

    CommandTarget="{Binding ElementName=textBox}"

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />

    <Button Command="Paste"

    CommandTarget="{Binding ElementName=textBox}"

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />

    <Button Command="Undo"

    CommandTarget="{Binding ElementName=textBox}"

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />

    <Button Command="Redo"

    CommandTarget="{Binding ElementName=textBox}"

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />

    <TextBox x:Name="textBox" Width="1000" />        

    </StackPanel>

    其中:

    Command="Cut":表示让命令"Cut"Button关联

    CommandTarget="{Binding ElementName=textBox}":指定引发命令的元素为textBox。也就是在是从textBox执行命令,而不是从Button执行。

    Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}":指定ButtonContent属性为命令的Text

    这个例子说明:虽然ButtonTextBox可以实现很多的交互,但它们互相并不了解,这也说明WPF的内建命令如此重要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值