WPF 附加属性的用法 (一)

时间:2023-03-08 18:30:49
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);
}
}
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
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);
}
}
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
public class LabelViewModel
{
public ICommand Command
{
get
{
return new RelayCommand((m) => MessageBox.Show(m.ToString() + "command双击事件成功"));
}
}
}
WPF 附加属性的用法 (一)
     <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),需要加上括号,这个比较特别,例如

WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
   <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">
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
<TextBox.Foreground>
<MultiBinding Converter="{StaticResource MaskBrushConverter}">
<Binding Path="Text" ElementName="tbUserName" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)" ElementName="tbUserName" />
</MultiBinding>
</TextBox.Foreground>
WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)

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

WPF 附加属性的用法 (一)
WPF 附加属性的用法 (一)
<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>
WPF 附加属性的用法 (一)
https://www.cnblogs.com/gaobw/p/6553443.html