【问题标题】:Using commands in wpf is not working properly for a button在 wpf 中使用命令对于按钮无法正常工作
【发布时间】:2016-10-02 13:01:28
【问题描述】:

下面xaml代码中有一个按钮:

<Button Grid.Row="1" x:Name="First" Content="First" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="40,91,0,67" />

在窗口的构造函数中,我已将ApplicationCommands.Copy 分配给按钮。

public MainWindow()
{ 
    InitializeComponent();

    this.doc.Unit = DevExpress.Office.DocumentUnit.Point;

    this.First.Command = ApplicationCommands.Copy;
    First.CommandTarget = this.doc;
}

上面代码中的this.doc是一个典型的richTextBox。 问题是按钮总是被禁用。为什么?
整个xaml代码如下:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:com="clr-namespace:DevExpress.XtraRichEdit.Commands;assembly=DevExpress.RichEdit.v16.1.Core"
    xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:ig="http://schemas.infragistics.com/xaml" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="700" Width="1000" x:Name="wnd">


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="163*"  />
        <RowDefinition Height="60*"  />
    </Grid.RowDefinitions>

    <dxre:RichEditControl x:Name="doc" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,23" Unit="Point" ActiveViewType="PrintLayout"/>

    <!--<telerik:RadToggleButton 
        Content="B" HorizontalAlignment="Left" Height="Auto" IsThreeState="False" IsChecked="False" Margin="10,32,0,0" Grid.Row="1" VerticalAlignment="Top" Width="26"/>-->

    <Button Grid.Row="1" x:Name="First" Content="First" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="40,91,0,67" />


</Grid></Window>


【问题讨论】:

  • 你能显示所有相关的 XAML 吗?可能是您的按钮位于因某种原因而被禁用的容器中。
  • @rory.ap 实际上我的 xaml 代码中只有那个按钮
  • 所以当您说“禁用”时,您的意思是“变灰”吗?或者是什么?即使您只有按钮,它也至少在一个 Window 控件中。
  • 实际上它是灰色的并且不工作,它是可见的。在 winForms 中它被称为禁用状态。

标签: c# wpf


【解决方案1】:

Button.Command 属性是一个类型 if ICommand,其中包括一个 CanExecute 方法。当此方法返回 false 时,Button 将被禁用。

我相信这就是这里发生的情况,ApplicationCommands.Copy 命令为 CanExecute 返回 false。也许其他人可以解释为什么会出现这种情况?

您能否按照here 的描述使用CommandBinding 来实现您自己的CanExecute 事件处理程序?

<Window x:Class="WCSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CloseCommand"
    Name="RootWindow"
    >
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Copy"
                    CanExecute="CanExecuteHandler"
                    />
  </Window.CommandBindings>
  <StackPanel Name="MainStackPanel">
    <Button Command="ApplicationCommands.Copy" 
            Content="Copy" />
  </StackPanel>
</Window>

private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
    // CanExecute logic in here.
    e.CanExecute = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多