zoukankan      html  css  js  c++  java
  • WPF 附加属性的用法 (一)

    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" 
                   Width="200" 
                   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}"
                   Height="30" 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
  • 相关阅读:
    ZROI 19.08.04模拟赛
    具体数学 第一章 递归问题
    ZROI 19.08.02 杂题选讲
    win下在虚拟机安装CentOS 7 Linux系统
    ICP算法(迭代最近点)
    Python学习笔记(一)
    堆和堆排序
    笔试面试题记录-
    笔试面试记录-字符串转换成整型数等(aatoi,itoa)
    支持向量机原理(一) 线性支持向量机
  • 原文地址:https://www.cnblogs.com/sjqq/p/8456994.html
Copyright © 2011-2022 走看看