0

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>


10
  • 1
    Can you show all the relevant XAML? It might be that your button is in a container that is disabled for some reason. Commented Oct 2, 2016 at 13:05
  • 1
    @rory.ap actually I just have that button in my xaml code Commented Oct 2, 2016 at 13:08
  • 1
    So when you say "disabled" do you mean "grayed out"? Or what? Even if you just have the button, it would be in at least a Window control. Commented Oct 2, 2016 at 13:10
  • 1
    actually it is grayed and not working, it is visible. In winForms it was called disabled state. Commented Oct 2, 2016 at 13:11
  • 2
    stackoverflow.com/questions/7563666/… Commented Oct 2, 2016 at 13:32

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.