WPF 附加属性的用法 (一)

摘要:
=null){if(e.NewValue!=null&&e.OldValue==null){control.MouseDoubleClick+=newMouseButton事件处理程序(control_MouseDouble Click);}elseif(e.NewValue==null&&eOldValue!
public class MDCTest
    {
        public static DependencyProperty MouseDoubleClickCommandProperty = DependencyProperty.RegisterAttached(
            "MouseDoubleClick",
            typeof(ICommand),
            typeof(MDCTest),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(MouseDoubleClickChanged))
            );
        public static void SetMouseDoubleClick(DependencyObject target, ICommand value)
        {
            target.SetValue(MDCTest.MouseDoubleClickCommandProperty, value);
        }
        public static ICommand GetMouseDoubleClick(DependencyObject target)
        {
            return (ICommand)target.GetValue(MDCTest.MouseDoubleClickCommandProperty);
        }
        private static void MouseDoubleClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            Control control = target as Control;
            if (control != null)
            {
                if (e.NewValue != null && e.OldValue == null)
                {
                    control.MouseDoubleClick += new MouseButtonEventHandler(control_MouseDoubleClick);
                }
                else if (e.NewValue == null && e.OldValue != null)
                {
                    control.MouseDoubleClick -= new MouseButtonEventHandler(control_MouseDoubleClick);
                }
            }
        }
        public static void control_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Control control = sender as Control;
            ICommand command = (ICommand)control.GetValue(MDCTest.MouseDoubleClickCommandProperty);
            command.Execute(control);
        }
    }
复制代码
复制代码
public class RelayCommand : ICommand
    {
        private Action<object> _Execute;
        private Predicate<object> _CanExecute;

        public RelayCommand(Action<object> execte)
            : this(execte, null)
        {
        }
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("Execute");
            _Execute = execute;
            _CanExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            return _CanExecute == null ? true : _CanExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _Execute(parameter);
        }
    }
复制代码
复制代码
public class LabelViewModel
    {
        public ICommand Command
        {
            get
            {
                return new RelayCommand((m) => MessageBox.Show(m.ToString() + "command双击事件成功"));
            }
        }
    }
复制代码
     <Label Name="label" local:MDCTest.MouseDoubleClick="{Binding Path=Command}">MouseDoubleClickTest</Label>

给Label附加双击事件 原文:http://www.cnblogs.com/ptfblog/archive/2011/07/11/2103183.html

绑定有两个需要注意的地方

1.如果绑定到 附加属性(Binding Attached Property),需要加上括号,这个比较特别,例如

复制代码
复制代码
   <TextBox x:Name="tbUserName" 
                 
               Grid.Column="0"
               Margin="10,10,0,0"         
               Foreground="Gray"
               nasSetting:TextBoxMaskHelper.MaskText="Please input username"
               Text="{Binding Path=(nasSetting:TextBoxMaskHelper.MaskText),Mode=OneWay,ElementName=tbUserName}"
                 CharacterCasing="Normal">
复制代码
复制代码
复制代码
复制代码
<TextBox.Foreground>
          <MultiBinding Converter="{StaticResource MaskBrushConverter}">
            <Binding Path="Text"   ElementName="tbUserName" UpdateSourceTrigger="PropertyChanged"/>
            <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)"  ElementName="tbUserName" />
          </MultiBinding>
        </TextBox.Foreground>
复制代码
复制代码

2.如果绑定到只读的属性(Binding to readonly property),例如IsFocused,需要加上 Mode = OneWay

复制代码
复制代码
<TextBox.Text>
          <MultiBinding Converter="{StaticResource MaskTextConverter}" Mode="OneWay">
            <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)"  ElementName="tbUserName" />
            <Binding Path="IsFocused"   ElementName="tbUserName"  Mode="OneWay"/>
          </MultiBinding>
        </TextBox.Text>
复制代码
 
https://www.cnblogs.com/gaobw/p/6553443.html

免责声明:文章转载自《WPF 附加属性的用法 (一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇类型信息redis 系列22 复制Replication (下)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF实现渐变淡入淡出的动画效果

1、实现原理 1.1 利用UIElement.OpacityMask属性,用于改变对象区域的不透明度的画笔。可以使元素的特定区域透明或部分透明,从而实现比较新颖的效果。 1.2 OpacityMask属性接受任何画刷,可利用LinearGradientBrush线性渐变画刷,通过对渐变画刷中各颜色点加以动画处理即可。 2、渐变淡入实现 渐变淡入效果,可通过...

WPF ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效,子控件拦截父控件滚动效果解决办法

注册子控件的PreviewMouseWheel,然后在滚动的时候设置控件的 IsHitTestVisible = false,因为这个参数设为false之后子控件里面的TextBox,CheckBox会失效,所以在停止滚动后要把参数设回来。代码如下 private void girdOrder_PreviewMouseWheel(object sende...

WPF 事件快速参考

为了提供快速参考,下面列出了 Expression Blend 中的可用事件。可用的事件可能会随着用户在“交互”面板的“对象和时间线”下选定不同的对象而有所变化。例如,如果在“对象和时间线”下选定“LayoutRoot”对象,则无法创建“Activated”事件处理程序方法,因为该事件仅对“Window”对象有效。 事件 描述 Annotatio...

WPF 查找控件的所有子控件

/// <summary> ///查找子控件 /// </summary> /// <typeparam name="T">控件类型</typeparam> /// <param name="parent">父控件依赖...

关于WPF中Popup控件的小记

在wpf开发中,常需要在鼠标位置处弹出一个“提示框”(在此就以“提示框”代替吧),通过“提示框”进行信息提示或者数据操作,如果仅仅是提示作用,使用ToolTip控件已经足够,但是有些是需要在弹出的框中有操作数据的功能,如弹出框包含一棵树或者列表,此时ToolTip就没法实现了,因为ToolTip只能起到显示的作用,也许有人会有弹出对话框的形式,这是一种办法...

wpf触发器

1.属性触发器(依赖属性皆可,有个疑问按钮点击一下一直在闪,待研究) <Style.Triggers><TriggerProperty="IsMouseOver"Value="True"><SetterProperty="Background"Value="Green"/></Trigger></Sty...