首先
ChannelSetupControl
类扩展
UserControl
,因此它隐式扩展
DependencyObject
班这意味着你可以使用
依赖项属性
而不是实施
INotifyPropertyChanged
.
因此,您可以在ChannelSetupControl类中定义依赖属性,如下所示:
public static readonly DependencyProperty UpCommandProperty =
DependencyProperty.Register("UpCommand", typeof(ICommand), typeof(ChannelSetupControl));
public ICommand UpCommand
{
get { return (ICommand)GetValue(UpCommandProperty); }
set { SetValue(UpCommandProperty, value); }
}
同时在控件XAML中:
<Button Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=UpCommand, Mode=OneWay}"
Content="+ 10" Padding="0 5" />
通过这种方式,您可以在窗口XAML中编写:
<local:ChannelSetupControl UpCommand="{Binding UpCommand, Mode=OneWay}" ... />
您可以对其他控件使用相同的“模式”。
关于
ICommand
,有很多实现。我更喜欢的是所谓的委托命令(对于示例,您可以查看
here
).
我希望这个简短的解释能对你有所帮助。