我有具有以下依赖属性WPF反向绑定OneWayToSource
public static readonly DependencyProperty PrintCommandProperty = DependencyProperty.Register(
"PrintCommand",
typeof(ICommand),
typeof(ExportPrintGridControl),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
public ICommand PrintCommand
{
get { return (ICommand)GetValue(PrintCommandProperty); }
set { throw new Exception("ReadOnly Dependency Property. Use Mode=OneWayToSource"); }
}
在我控制的构造,我设置我的属性的默认值的自定义控制:
public MyControl()
{
this.SetValue(PrintCommandProperty, new DelegateCommand<object>(this.Print));
}
我然后试图将该属性绑定到我的ViewModel,以便我可以访问该属性并调用打印命令。
<controls:MyControl PrintCommand="{Binding PrintCommand, Mode=OneWayToSource}"/>
但是,在XAML中的绑定会导致属性值设置为null。如果我删除XAML中的绑定,则默认属性值在我的控件的构造函数中正确设置。
让我的ViewModel调用我的控件的Print方法的正确方法是什么?