我有具有以下依賴屬性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方法的正確方法是什麼?