- 浏览: 389486 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
It is always useful to have some common stereotype/boilerplate so that you can copy and paste and start something every quickly.
This is for RoutedCommand stenciles/templates.
there are three major steps involves with the RoutedCommand;
they are
- Declare/Define/Create the routed Command
- Set the Command property a Control can raise/trigger, e.g. <Button Command=...>
- Add/Remove the handler to the RoutedCommand, through the use CommandBinding
In the following example, we are going to create Custom RoutedCommand, and we are going to give it name "OKCommand";
The RoutedComand Static Field Approach
Since in Xaml the CommandBinding's Command Property is a static field, you could not use a Binding Element on the Command attribute of CommandBindings;
So it is recommended to use Static Field for the RoutedCommands;
private static RoutedCommand m_okCommand; public static RoutedCommand OKCommand { get { return m_okCommand != null ? m_okCommand : (m_okCommand = new RoutedCommand("OKCommand", typeof(MainWindow))); } }
and you use the x:Static Xaml directive to pick up the ICommand
<Button x:Name="OKButton" IsDefault="True" Content="OK" Command="{x:Static local:MainWindow.OKCommand}" CommandTarget="{Binding RelativeSource={RelativeSource Self}}"> <Button.CommandBindings> <CommandBinding Command="{x:Static local:MainWindow.OKCommand}" CanExecute="OKCommandCanExecute" Executed="OKCommandExecuted" > </CommandBinding> </Button.CommandBindings> <Button.InputBindings> <KeyBinding Gesture="Alt+O" Command="{x:Static local:MainWindow.OKCommand}"> </KeyBinding> </Button.InputBindings> </Button>
Except setting the command bindings through the use of Xaml, you can also do it via code inside the CLR code. Below is one example.
public MainWindow() { InitializeComponent(); this.CommandBindings.Add(new CommandBinding( OKCommand, OKCommandExecuted, OKCommandCanExecute) ); }
As you can see, this is the most common way of using the RoutedCommand.
A slightly better way is to move the RoutedCommand out of the MainWindow and put into a separate class which can be shared by many classes.
Following the discussion, we will move the commands to another class called Commands;
namespace AcceptOrCancelButton { public class Commands : DependencyObject { private static RoutedCommand m_okCommand; public static RoutedCommand OKCommand { get { return m_okCommand != null ? m_okCommand : (m_okCommand = new RoutedCommand("OKCommand", typeof(MainWindow))); } } } }
The Dependency Property approach
DependencyProperty the key is a public static readonly field, the metadata is associated with the owner class as well, but the CLR accessor is associated with the class intance, and this is creating a issue for us iin Xaml.
Suppose now the Commands class is like this:
namespace AcceptOrCancelButton { public class Commands : DependencyObject { public RoutedCommand OKCommand { get { return (RoutedCommand)GetValue(OKCommandProperty); } set { SetValue(OKCommandProperty, value); } } // Using a DependencyProperty as the backing store for OKCommand. This enables animation, styling, binding, etc... public static readonly DependencyProperty OKCommandProperty = DependencyProperty.Register("OKCommand", typeof(RoutedCommand), typeof(Commands), new UIPropertyMetadata(new RoutedCommand("OKCommand", typeof(Commands)))); } }
In xaml , the following won't work.
<Window x:Class="AcceptOrCancelButton.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AcceptOrCancelButton" x:Name="Self" > <Window.CommandBindings> <CommandBinding Command="{Binding Path=OKCommand, ElementName=Self}" CanExecute="OKCommandCanExecute" Executed="OKCommandExecuted" > </CommandBinding> </Window.CommandBindings> </Window>
You will see the following error at runtime.
A 'Binding' cannot be set on the 'Command' property of type 'CommandBinding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
however, you can still use the code to do the bindings;
namespace AcceptOrCancelButton { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.CommandBindings.Add(new CommandBinding( Commands.OKCommand, OKCommandExecuted, OKCommandCanExecute) ); } private Commands m_commands = new Commands(); public Commands Commands { get { return m_commands; } } } }
the UIElement do has the Command as DependencyProperty, so that you can do the following.
<Button x:Name="OKButton" IsDefault="True" Content="OK" Command="{Binding Path=Commands.OKCommand, ElementName=Self}" CommandTarget="{Binding RelativeSource={RelativeSource Self}}"> <Button.InputBindings> <KeyBinding Gesture="Alt+O" Command="{Binding Path=Commands.OKCommand, ElementName=Self}"> </KeyBinding> </Button.InputBindings> </Button>
However, if you do want to achieve this in Xaml (if you are paranoid for no reason).
the key is the StaticResource
<Window x:Class="AcceptOrCancelButton.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AcceptOrCancelButton" Title="MainWindow" Height="350" Width="525" x:Name="Self" > <Window.Resources> <RoutedCommand x:Key="MyCommand" /> <local:Commands x:Key="Commands" OKCommand="{StaticResource MyCommand}" > <!--<local:Commands.OKCommand> <StaticResource ResourceKey="MyCommand"></StaticResource> </local:Commands.OKCommand>--> </local:Commands> </Window.Resources> <StackPanel> <!-- With the help of Click Event, prove cancel and default button works --> <!--<Button x:Name="OKButton" IsDefault="True" Content="OK" Click="OkClick"/> <Button x:Name="CancelButton" IsCancel="True" Content="Cancel" Click="CancelClick"/>--> <!-- With the help of Static Routed Comand, prove cancel and default button works --> <Button x:Name="OKButton" IsDefault="True" Content="OK" Command="{StaticResource MyCommand}" CommandTarget="{Binding RelativeSource={RelativeSource Self}}"> <Button.CommandBindings> <CommandBinding Command="{StaticResource MyCommand}" CanExecute="OKCommandCanExecute" Executed="OKCommandExecuted" > </CommandBinding> </Button.CommandBindings> <Button.InputBindings> <KeyBinding Gesture="Alt+O" Command="{StaticResource MyCommand}"> </KeyBinding> </Button.InputBindings> </Button> </StackPanel> </Window>
You can only use StaticResource, you cannot use DynamicResource, because DynamicResource requires that the target property is a DependencyProperty;
Tricks to use AttachedDependencyProperty
You can create a Attached Dependency Property,which indicate if the element that the attached property is applied will be bound to a known RoutedCommand (in this example), the known RoutedCommand is the defined in the Resources.
here is the code.
public static bool GetIsOKCommandSource(DependencyObject obj) { return (bool)obj.GetValue(IsOKCommandSourceProperty); } public static void SetIsOKCommandSource(DependencyObject obj, bool value) { obj.SetValue(IsOKCommandSourceProperty, value); } // Using a DependencyProperty as the backing store for IsOKCommandSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsOKCommandSourceProperty = DependencyProperty.RegisterAttached("IsOKCommandSource", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false, OnIsOkCommandSourceChanged)); public static void OnIsOkCommandSourceChanged(DependencyObject dep, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue == true) { //Button button = dep as Button; Button button = dep as Button; if (button != null) { var command = ((Commands)button.TryFindResource("Commands")).OKCommand; button.Command = command; button.CommandTarget = button; } } }
and you have the following xaml which basically create the resource, create the Command handlers, and use the attach dependency property.
<Window x:Class="AcceptOrCancelButton.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AcceptOrCancelButton" Title="MainWindow" Height="350" Width="525" x:Name="Self" > <Window.Resources> <RoutedCommand x:Key="MyCommand" /> <local:Commands x:Key="Commands" OKCommand="{StaticResource MyCommand}" > </local:Commands> </Window.Resources> <Window.CommandBindings> <CommandBinding Command="{StaticResource MyCommand}" CanExecute="OKCommandCanExecute" Executed="OKCommandExecuted" > </CommandBinding> </Window.CommandBindings> <StackPanel> <Button x:Name="OKButton" local:MainWindow.IsOKCommandSource="True" IsDefault="True" Content="OK"> </Button> </StackPanel> </Window>
Another way to use the Attached Dependency Property is as follow. -- this is not IDEAL, tend to remove
public static RoutedCommand GetOkCommand(DependencyObject obj) { return (RoutedCommand)obj.GetValue(OkCommandProperty); } public static void SetOkCommand(DependencyObject obj, RoutedCommand value) { obj.SetValue(OkCommandProperty, value); } // Using a DependencyProperty as the backing store for OkCommand. This enables animation, styling, binding, etc... public static readonly DependencyProperty OkCommandProperty = DependencyProperty.RegisterAttached("OkCommand", typeof(RoutedCommand), typeof(MainWindow), new UIPropertyMetadata(null, OnOKCommandChanged)); public static void OnOKCommandChanged(DependencyObject dep, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null) { if (dep is ICommandSource) { var parent = GetParent(dep, typeof(MainWindow)) as MainWindow; if (dep.GetType() == typeof(Button)) { Button button = (Button)dep; button.Command = (RoutedCommand)e.NewValue; button.CommandTarget = button; button.CommandBindings.Add(new CommandBinding(button.Command, parent.OKCommandExecuted, parent.OKCommandCanExecute)); } } } } public static DependencyObject GetParent(DependencyObject obj, Type parentType) { DependencyObject parent = obj ; if (obj == null) return null; Type type = obj.GetType(); while (type != parentType) { parent = LogicalTreeHelper.GetParent(parent); if (parent != null) type = parent.GetType(); else return null; } return parent; }
and the xaml
<Window x:Class="AcceptOrCancelButton.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AcceptOrCancelButton" Title="MainWindow" Height="350" Width="525" x:Name="Self" > <Window.Resources> <RoutedCommand x:Key="MyCommand" /> <local:Commands x:Key="Commands" OKCommand="{StaticResource MyCommand}" > </local:Commands> </Window.Resources> <StackPanel> <Button x:Name="OKButton" local:MainWindow.OkCommand="{Binding Source={StaticResource MyCommand}}" IsDefault="True" Content="OK" /> </StackPanel> </Window>
The last attached event I am thinking of is of type CommandBinding; Please see below.
public static CommandBinding GetOKCommandBinding(DependencyObject obj) { return (CommandBinding)obj.GetValue(OKCommandBindingProperty); } public static void SetOKCommandBinding(DependencyObject obj, CommandBinding value) { obj.SetValue(OKCommandBindingProperty, value); } // Using a DependencyProperty as the backing store for OKCommandBinding. This enables animation, styling, binding, etc... public static readonly DependencyProperty OKCommandBindingProperty = DependencyProperty.RegisterAttached("OKCommandBinding", typeof(CommandBinding), typeof(MainWindow), new UIPropertyMetadata(null, OnOKCommandBindingChanged)); public static void OnOKCommandBindingChanged(DependencyObject dep, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null) { if (dep.GetType() == typeof(Button)) { var button = (Button)dep; button.CommandBindings.Clear(); button.CommandBindings.Add((CommandBinding)e.NewValue); } } }
and the Xaml
<Window x:Class="AcceptOrCancelButton.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AcceptOrCancelButton" Title="MainWindow" Height="350" Width="525" x:Name="Self" > <Window.Resources> <RoutedCommand x:Key="MyCommand" /> <local:Commands x:Key="Commands" OKCommand="{StaticResource MyCommand}" > </local:Commands> <CommandBinding x:Key="OKCommandBinding" Command="{StaticResource MyCommand}" Executed="OKCommandExecuted" CanExecute="OKCommandCanExecute" > </CommandBinding> </Window.Resources> <StackPanel> <Button x:Name="OKButton" local:MainWindow.OKCommandBinding="{StaticResource OKCommandBinding}" Command="{StaticResource MyCommand}" IsDefault="True" Content="OK" /> </StackPanel> </Window>
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1939first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1361Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1279Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1880You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5292As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1144Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 770We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2820I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3544Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1253Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2127It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1428The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7369In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1010As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1360As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 834A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3655This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3544The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 935In previous discussion, we have ...
相关推荐
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
c#-的WPF---MVVM例子,大家共同学习
一个简单的WPF程序
bootstrap-wpf-style-master 样式代码 。。。。。。。。
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
Prism-Samples-Wpf-master06-10的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习
wpf--ListBox分组-排序 wpf--ListBox分组-排序
我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其它的MVVM框架,比如浅谈WPF中的MVVM框架--MVVMFoundation 中提到的MVVMFoundation,再比如 ViewModel从未如此清爽 - 轻量级...
wpf-wcf-wf整合开发实例--有利于理解wpf-wcf-wf的整合
WPF收集程序
WPF的基本空间历程,使用.net core3.0.1版本
WPF--Effect特效 对Effect的进行使用的小例子
wpf-datagrid-access DB,可以更新数据到Access数据库
wpf-资源的使用-resource
Wpf-图片放大缩小移动操作 Wpf-图片放大缩小移动操作
wpf charts源码,D源码,图书:practical-wpf-charts-graphics-master
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
WPF-MVVM WPF MVVM模式开发的例子程序
超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...
WPF-Diagram-Designer:WPF图表设计器源代码