Can't figure out why button is disabled after adding a command

1.1k Views Asked by At

Edit: Here is the xaml for the window containing the command bindings.

<dx:DXWindow
    x:Class="Client.App.Support.AskAQuestionDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:libRes="clr-namespace:Shared.Lib.Resources;assembly=Shared.Lib"
    xmlns:support="clr-namespace:Client.App.Support"
    Title="{x:Static libRes:Strings.AskAQuestion}" Loaded="DXWindow_Loaded" 
    Height="260" Width="600">

<Window.CommandBindings>
    <CommandBinding Command="support:AskAQuestionDialog.ListToSendCommand" Executed="MainWindowCommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</Window.CommandBindings>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Vertical">
        <TextBlock Style="{StaticResource DatailsHeaderTextStyle}" Margin="4,4,4,4" Text="{x:Static libRes:Strings.Subject}"/>
        <TextBox Name="_subjectTextBox" AcceptsReturn="False" TextChanged="_subjectTextBox_TextChanged" Margin="2" MaxLines="1" TextWrapping="NoWrap"/>
        <TextBlock Style="{StaticResource DatailsHeaderTextStyle}" Margin="4,4,4,4" Text="{x:Static libRes:Strings.Description}"/>
        <TextBox VerticalAlignment="Stretch" Name="_descriptionTextBox" VerticalScrollBarVisibility="Auto" TextWrapping="WrapWithOverflow" AcceptsReturn="True" TextChanged="_descriptionTextBox_TextChanged"/>
    </StackPanel>

    <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
        <StackPanel.Resources>
            <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
                <Setter Property="Width" Value="80"/>
                <Setter Property="Margin" Value="2"/>
            </Style>
        </StackPanel.Resources>
        <Button Name="Attach" Content="Attach Screen Shots" Click="Attach_Click" Width="140" HorizontalAlignment="Right"/>
        <Button Content="{x:Static libRes:Strings.Submit}" Click="Submit_Click" Margin="10,0,0,0"/>
        <Button Content="{x:Static libRes:Strings.Close}" Click="Close_Click" Margin="10,0,0,0"/>
        <Grid>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
                <ItemsControl Name="_itemsControl" ItemsSource="{Binding ''}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Image Name ="_thumbnailImage" HorizontalAlignment="Left" VerticalAlignment="Center" Source="{Binding ''}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>
    </StackPanel>
</Grid>

And the relevant code behind:

private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ListToSendCommand)
    {
        this._itemsControl.ItemsSource = (List<BitmapSource>)e.Parameter;
    }
}

I've been learning about RoutedCommands in WPF and I ran into a problem after adding a command to a button. In my window, SelectScreenShots, I have a single CommandBinding that is handled in the code behind.

I have another window, AskAQuestionDialog, with another command binding that is handled in its code behind.

In SelectScreenShots I added the command that is handled in AskAQuestion to a button and now the button is constantly disabled. Before when I was just using a click event, it worked fine.

Why is the button now disabled?

Here is the xaml. The command I added is ListToSendCommand on the button _OK_Button.

<dxc:DXWindow x:Class="Client.App.Support.SelectScreenShots"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" Focusable="False" IsTabStop="False"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:libRes="clr-namespace:Shared.Lib.Resources;assembly=Shared.Lib"
    xmlns:support="clr-namespace:Client.App.Support"
    Title="Select Images" Height="600" Width="800">

<Window.CommandBindings>
    <CommandBinding Command="support:SelectScreenShots.SelectImageCommand" Executed="MainWindowCommandBinding_Executed"/>
</Window.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="367"/>
            <RowDefinition Height="167"/>
            <RowDefinition Height="33"/>
        </Grid.RowDefinitions>
        <ContentPresenter Grid.Row="0" Name="_contentPresenter" Content="{Binding ''}"/>
        <ContentPresenter Grid.Row="1" Name="_contentPresenter2" Content="{Binding ''}"/>
        <StackPanel Grid.Row="2" HorizontalAlignment="Right"  Orientation="Horizontal">
        <Button Name="_OK_Button" Content="OK" Margin="0,5,5,5" Width="75" Height="23" Command="{x:Static support:AskAQuestionDialog.ListToSendCommand}" CommandParameter="{Binding ''}" 
                IsEnabled="True"/>
        <Button Name="_Cancel_Button" Content="Cancel" Click="_Cancel_Button_Click" Margin="0,5,5,5" Width="75" Height="23"/>
        </StackPanel>
    </Grid>

2

There are 2 best solutions below

2
On

I suspect ListToSendCommand have CanExecute delegate associated with it which returns false. Hence, you see a disabled button.

0
On
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

Modify your CanExecuteChanged event as above to listen to the Command Manager notification. This will raise the CanExecute method when there are any UI changes like focus changes, editing the textbox, etc. This will evaluate the logic inside your CanExecute method and if returns true, your button would be enabled.