无法确定添加命令后禁用按钮的原因

时间:2013-12-24 16:47:31

标签: c# wpf routed-commands

编辑:这是包含命令绑定的窗口的xaml。

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

背后的相关代码:

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

我已经在WPF中了解RoutedCommands,并且在向按钮添加命令后遇到了问题。在我的窗口SelectScreenShots中,我有一个CommandBinding在后​​面的代码中处理。

我有另一个窗口AskAQuestionDialog,另一个命令绑定在后面的代码中处理。

SelectScreenShots中,我将AskAQuestion中处理的命令添加到按钮,现在按钮一直被禁用。在我刚刚使用点击事件之前,它运行良好。

为什么按钮现已停用?

这是xaml。我添加的命令是按钮ListToSendCommand上的_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>

1 个答案:

答案 0 :(得分:3)

我怀疑ListToSendCommandCanExecute委托关联,并返回false。因此,您会看到一个禁用按钮。