[WPF问题] Command 对部分UI element 不起作用

wxtp 2009-03-11 02:40:04
代码如下:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace WpfApplication1
{
public class Window1: Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new Window1());
}

public Window1()
{
Title = "Window1";
Width = 300;
Height = 300;

DockPanel panel = new DockPanel();
Content = panel;

ToolBar toolbar = new ToolBar();
toolbar.Height = 30;
Button toolbutton = new Button();
toolbutton.Content = "Cut";
toolbutton.Command = ApplicationCommands.Cut;
toolbar.Items.Add(toolbutton);
toolbar.SetValue(DockPanel.DockProperty, Dock.Top);
panel.Children.Add(toolbar);

TextBox text = new TextBox();
text.Width = 80;
text.Height = 20;
text.SetValue(DockPanel.DockProperty, Dock.Top);
panel.Children.Add(text);



Button button = new Button();
button.Content = "Copy";
button.Command = ApplicationCommands.Copy;
button.VerticalAlignment = VerticalAlignment.Top;
button.HorizontalAlignment = HorizontalAlignment.Center;
button.SetValue(DockPanel.DockProperty, Dock.Top);
panel.Children.Add(button);
}
}
}


现在的问题是对于toolbutton 和 button分别绑定了Cut和Copy命令,而在textbox中选中输入文字的时候,toolbutton由disable状态变成enable状态,但是button却始终是disable状态。
请问一下大家这是什么原因?如果要使button变成enable,应该怎么改呢?
...全文
199 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxtp 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cppfaq 的回复:]
引用 2 楼 wxtp 的回复:
我对Copy命令添加了CommandBinding后正常。


C# code
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,CopyOnExcecute, CopyCanExcecute));


C# code
void CopyCanExcecute(object sender, CanExecuteRoutedEventArgs args)
{
args.CanExecute = !String.IsNullOrEmpty(text.Text);
}

void CopyOnExcecute(object sender, ExecutedRoutedEventArgs args)…
[/Quote]
应该说是WPF类帮我们做了些事,但是,Command是怎么被传递的。同样是Button,为什么ToolBar上的Button收到Command,panel上的没有收到,有熟悉Command机制的朋友能解释一下吗?
luckapple2 2009-03-11
  • 打赏
  • 举报
回复
同意。toolbutton已经封装了。你当然不用再写了。
cppfaq 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxtp 的回复:]
我对Copy命令添加了CommandBinding后正常。


C# code
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,CopyOnExcecute, CopyCanExcecute));




C# code
void CopyCanExcecute(object sender, CanExecuteRoutedEventArgs args)
{
args.CanExecute = !String.IsNullOrEmpty(text.Text);
}

void CopyOnExcecute(object sender, ExecutedRoutedEventArgs args)
{
Clipboard.SetText(…
[/Quote]
因为VS悄悄地帮你做了
wxtp 2009-03-11
  • 打赏
  • 举报
回复
我对Copy命令添加了CommandBinding后正常。


CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,CopyOnExcecute, CopyCanExcecute));


void CopyCanExcecute(object sender, CanExecuteRoutedEventArgs args)
{
args.CanExecute = !String.IsNullOrEmpty(text.Text);
}

void CopyOnExcecute(object sender, ExecutedRoutedEventArgs args)
{
Clipboard.SetText(text.Text);
}


可问题是为什么toolbutton不用command binding就可以正常响应Command,而button需要添加command binding?

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧