111,092
社区成员




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);
}
}
}
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);
}