C# WPF文本框命令绑定
我使用两个文本框和“保存”按钮。基本上,“保存”按钮将在TextBox有任何文本更改时启用。我在C# WPF文本框命令绑定,c#,wpf,C#,Wpf,我使用两个文本框和“保存”按钮。基本上,“保存”按钮将在TextBox有任何文本更改时启用。我在窗口中创建了一个CommandBinding。Resource和“Save”按钮使用Command=“Save”和两个文本框使用StaticResources进行命令绑定 但是,当我更改文本时,按钮未启用。使用Debug,我可以看到TextBox文本更改的标志是True,但看起来TextBox没有触发Save命令CanExecuted事件 下面是我的代码 xaml ... 暗码 public分部类
窗口中创建了一个CommandBinding
。Resource
和“Save”按钮使用Command=“Save”
和两个文本框使用StaticResources
进行命令绑定
但是,当我更改文本时,按钮未启用。使用Debug,我可以看到TextBox文本更改的标志是True
,但看起来TextBox没有触发Save
命令CanExecuted
事件
下面是我的代码
xaml
...
暗码
public分部类UseCommand:Window
{
专用词典_isDirty=新词典();
公用命令()
{
初始化组件();
此.AddHandler(CommandManager.PreviewExecutedEvent,
新执行的RoutedEventHandler(CommandExecuted));
}
私有无效TbInputText\u OnTextChanged(对象发送者,TextChangedEventArgs e)
{
//_isDirty.Add(发送方,true);
_isDirty[发送方]=真;
}
#区域保存
私有void SaveCommand_CanExecute(对象发送方,canexecuterouteEventArgs e)
{
if(_isDirty.ContainsKey(发送方)&&&u isDirty[发送方])
{
e、 CanExecute=true;
}
其他的
{
//Show(sender.ToString());
e、 CanExecute=false;
}
}
私有void SaveCommand_已执行(对象发送方,已执行路由目标)
{
字符串文本=((文本框)发送者).text;
MessageBox.Show(“关于此控制器:”+sender.ToString()+
“内容:”+文本);
_isDirty[发送方]=假;
}
#端区
}
有没有我错过的一步?为什么CanExecuted
没有被触发
您必须将save CommandBinding从集合移动到集合
删除x:Key属性
将按钮的命令属性设置为“ApplicationCommand.Save”
(...)
(...)
(...)
嘿。谢谢你的更新。我可以做到。但是,有两个TextBox也在使用保存
命令。所以保存按钮在不同的TextBox上会有不同的活动。哦……我想我发现了。如果你使用而不是这些按钮,它实际上可以工作。当TextBox的文本发生更改时,它会启用保存按钮n、 你知道原因是什么吗?首先,我不认为TextBox控件可以调用命令,这就是为什么不调用SaveCommand\u CanExecute
和SaveCommand\u Executed
的原因
<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>
<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"
/>
(...)