首页 > 解决方案 > WPF 文本框命令绑定

问题描述

我正在使用两个文本框和“保存”按钮。基本上,当 TextBox 更改任何文本时,将启用“保存”按钮。我CommandBindingWindow.Resource和“保存”Button用途中创建了一个Command="Save"和两个 TextBoxStaticResources用于命令绑定。

但是,当我更改文本时,按钮未启用。使用调试,我可以看到我的 TextBox 文本标记已更改,True但看起来 TextBox 没有触发Save命令CanExecuted事件。

下面是我的代码。

xml

<Window>
<Window.CommandBindings>
            <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed" />
            <CommandBinding Command="{x:Static commands:DataCommands.Requery}" Executed="RequeryCommand_Executed"/>
            <CommandBinding Command="{x:Static commands:DataCommands.ApplicationUndo}" 
                            Executed="ApplicationUndo_OnExecuted" CanExecute="ApplicationUndo_OnCanExecute"/>
    </Window.CommandBindings>

    <Window.Resources>
        <CommandBinding x:Key="Binding" Command="ApplicationCommands.Save"
                        Executed="SaveCommand_Executed" CanExecute="SaveCommand_CanExecute"/>
    </Window.Resources>

    <StackPanel>
        <Menu>
            <MenuItem Header="File">
                <MenuItem Command="New"/>
            </MenuItem>
        </Menu>

        <StackPanel Orientation="Horizontal" Margin="5">
            <Button Name="New"  Command="New" Content="New" Margin="3" Padding="3"/>
            <Button Name="Save" Command="Save" Content="Save"  Margin="3" Padding="3"/>
            ...
        </StackPanel>
        
        <TextBox Name="TbInputText1" TextChanged="TbInputText_OnTextChanged" Margin="5">
            <TextBox.CommandBindings>
                <StaticResource ResourceKey="Binding"/>
            </TextBox.CommandBindings>
        </TextBox>

        <TextBox Name="TbInputText2" Margin="5" TextChanged="TbInputText_OnTextChanged">
            <TextBox.CommandBindings>
                <StaticResource ResourceKey="Binding"/>
            </TextBox.CommandBindings>
        </TextBox>

        <ListBox Name="LsbHistory" DisplayMemberPath="Name" Margin="3"></ListBox>

    </StackPanel>

隐藏代码

   public partial class UseCommand : Window
{
    private Dictionary<Object, bool> _isDirty = new Dictionary<Object, bool>();

    public UseCommand()
    {
        InitializeComponent();
        this.AddHandler(CommandManager.PreviewExecutedEvent,
            new ExecutedRoutedEventHandler(CommandExecuted));
    }


    private void TbInputText_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        // _isDirty.Add(sender, true);
        _isDirty[sender] = true;
    }

    #region Save 
    private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (_isDirty.ContainsKey(sender) && _isDirty[sender])
        {
            e.CanExecute = true;
        }
        else
        {
            // MessageBox.Show(sender.ToString());
            e.CanExecute = false;
        }
    }


    private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        string text = ((TextBox)sender).Text;
        MessageBox.Show("About this controller: " + sender.ToString() +
                        "Contents: " + text);
        _isDirty[sender] = false;
    }
    #endregion
}

我错过了什么步骤吗?为什么CanExecuted没有触发?

标签: c#wpf

解决方案


  1. 您必须将保存的 CommandBinding 从 <Window.Resource> 集合移至 <Window.CommandBindings> 集合
  2. 删除 x:Key 属性
  3. 将按钮的 Command 属性设置为 ""ApplicationCommands.Save"
<Window.CommandBindings>
   <CommandBinding
       CanExecute="SaveCommand_CanExecute"
       Command="ApplicationCommands.Save"
       Executed="SaveCommand_Executed"/>
(...)
</Window.CommandBindings>
<StackPanel>
(...)

<Button
   Name="Save"
   Margin="3"
   Padding="3"
   Command="ApplicationCommands.Save"
   Content="Save"
/>
(...)

推荐阅读