【问题标题】:Can't figure out why button is disabled after adding a command添加命令后无法弄清楚为什么按钮被禁用
【发布时间】:2013-12-24 16:47:30
【问题描述】:

编辑:这是包含命令绑定的窗口的 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。我添加的命令是按钮_OK_Button上的ListToSendCommand

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

【问题讨论】:

  • ListToSendCommand 的邮政编码。

标签: c# wpf routed-commands


【解决方案1】:

我怀疑ListToSendCommand 有与之关联的CanExecute 委托,它返回false。因此,您会看到一个禁用的按钮。

【讨论】:

  • 我添加了一个可以执行的事件处理程序并将其设置为 true,但它仍然被禁用。我不完全理解 CanExecute 是如何工作的?它的广泛功能是启用或禁用命令吗?你如何触发这个事件?
  • 在这种情况下你可以为你的命令发布代码吗?请更新您的问题。
【解决方案2】:
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

如上修改您的CanExecuteChanged 事件以收听Command Manager 通知。当有任何 UI 更改(如焦点更改、编辑文本框等)时,这将引发 CanExecute 方法。 这将评估您的 CanExecute 方法中的逻辑,如果返回 true,您的按钮将被启用。

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2021-05-29
    相关资源
    最近更新 更多