【问题标题】:WPF Copy button issueWPF复制按钮问题
【发布时间】:2012-09-27 17:01:10
【问题描述】:

我的应用程序中有复制粘贴按钮。最初必须禁用两个按钮,如果我从任何文本框中选择任何文本,则只需启用复制按钮。一旦我复制了一些东西,就需要启用粘贴按钮。我在 App.xaml.cs 文件中使用此代码:

我的问题是,我怎么知道我什么时候必须启用这个按钮,如何在 CanCmdCopy 功能中找出我是否选择了任何文本?

    #region CopyCommand
    private void CmdCopy(object sender, ExecutedRoutedEventArgs args)
    {
        // code///
    }
    private void CanCmdCopy(object sender, CanExecuteRoutedEventArgs args)
    {
        args.CanExecute = ?????
    }
    #endregion

谢谢

【问题讨论】:

    标签: wpf copy-paste commandbinding


    【解决方案1】:

    您应该使用默认的ApplicationCommands,它们将具有您正在寻找的行为。如果您使用ToolBar,它将工作而无需执行任何其他操作,否则您将不得不使用FocusManager.IsFocusScope 属性或直接绑定CommandTarget。请注意,如果剪贴板中有任何内容,则粘贴按钮将被启用。您可以使用ClipBoard.Clear 方法重置剪贴板。

    即:

    示例 1

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <StackPanel >
        <ToolBar>
            <Button  Command="ApplicationCommands.Copy">Copy</Button>
            <Button Command="ApplicationCommands.Paste">Paste</Button>
        </ToolBar>
        <TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
        <TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
    
      </StackPanel >
    
    </Window>
    

    示例 2

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <StackPanel >
          <TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
          <TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
          <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FocusManager.IsFocusScope="True" >
              <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <Button Grid.Column="0" Height="50" Command="ApplicationCommands.Copy" >Copy</Button>
              <Button Grid.Column="1" Height="50" Command="ApplicationCommands.Paste">Paste</Button>
          </Grid >
      </StackPanel >
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 2012-05-31
      • 2011-02-10
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多