c# - WPF 文本框命令绑定
问题描述
我正在使用两个文本框和“保存”按钮。基本上,当 TextBox 更改任何文本时,将启用“保存”按钮。我CommandBinding
在Window.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
没有触发?
解决方案
- 您必须将保存的 CommandBinding 从 <Window.Resource> 集合移至 <Window.CommandBindings> 集合
- 删除 x:Key 属性
- 将按钮的 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"
/>
(...)
推荐阅读
- amazon-web-services - curl命令在aws ec2实例上给出'curl:(6)无法解析主机:XXX.XXX.XXX'错误
- css - 如何修复 html 表中的标题,其中包含具有行跨度和 coll-span 的多行标题
- reactjs - 如何使用护照和护照-jwt 正确保护我的所有路线
- c# - 无论是在 web.config 中还是在代码中,我应该在哪里包含 MetaData 文件?
- billing - 使用提供的客户端/UI 对 api 调用进行计费
- vb.net - 为有理位的属性项写入 Exif 元数据 (rational64u)
- preview - 如何让预览在 SwiftUI 项目中再次工作
- r - 将一个数据集的零值替换为 R 中第二个数据集的最后一个值
- java - 如何在 Java8 中进行分组和归约
- python - Pandas 的 to_datetime 函数不会改变 dtype