using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace WpfApplication1 {
public class UICommands {
public static RoutedUICommand ChangeBGColorCommand =
new RoutedUICommand ( );
}
}
2.在XAML Window 中加入命名空間using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace WpfApplication1 {
public class UICommands {
public static RoutedUICommand ChangeBGColorCommand =
new RoutedUICommand ( );
}
}
xmlns:cmd="clr-namespace:WpfApplication1"
3.加Window.CommandBindings
<Window.CommandBindings>
<CommandBinding x:Name="changebg" Executed="changebg_Executed"
Command="cmd:UICommands.ChangeBGColorCommand"
/>
</Window.CommandBindings>
<CommandBinding x:Name="changebg" Executed="changebg_Executed"
Command="cmd:UICommands.ChangeBGColorCommand"
/>
</Window.CommandBindings>
4.寫changebg_Executed 事件
private void changebg_Executed ( object sender , ExecutedRoutedEventArgs e ) {
this.button1.Background = new SolidColorBrush ( Colors.Red );
}
this.button1.Background = new SolidColorBrush ( Colors.Red );
}
5.在Window加入StackPanel 、Menu、MenuItem 、Button
<StackPanel >
<Menu>
<MenuItem Header="_Menu" >
<MenuItem Header="Change "
Command="cmd:UICommands.ChangeBGColorCommand"
/>
</MenuItem>
</Menu>
<Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>
<Menu>
<MenuItem Header="_Menu" >
<MenuItem Header="Change "
Command="cmd:UICommands.ChangeBGColorCommand"
/>
</MenuItem>
</Menu>
<Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>
6.Run
沒有留言:
張貼留言