Green Falcon
Green Falcon

Reputation: 836

Using commands in wpf is not working properly for a button

There is a button in the following xaml code:

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

In the constructor of the window I have assigned ApplicationCommands.Copy to the button.

public MainWindow()
{ 
    InitializeComponent();

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

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

this.doc in the above code is a typical richTextBox. The problem is that the button is always disabled. Why?
The entire xaml code is as follows:

<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>


Upvotes: 0

Views: 522

Answers (1)

Simon
Simon

Reputation: 326

The Button.Command property is a type if ICommand, which includes a CanExecute method. When this method returns false the Button will be disabled.

I believe that's what's happening here, the ApplicationCommands.Copy command is returning false for CanExecute. Perhaps someone else can offer an explanation as to why this is in this case?

Are you able to use a CommandBinding as described here to implement your own CanExecute event handler?

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

Upvotes: 2

Related Questions