用户自定义控件依赖属性Command的绑定
自定义用户控件增加Command依赖属性并绑定
自定义控件 IconButton.cs中增加依赖属性
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(IconButton), new PropertyMetadata(null));
<UserControl x:Class="EasyCode.Controls.IconButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="me" >
<Button Command="{Binding Command,ElementName=me}">Click Me!</Button>
</UserControl>
在父窗口中设置如下绑定:
<my:GreatUserControl Command="{Binding SomeCommand}" />