【发布时间】:2015-11-30 13:40:11
【问题描述】:
我正在做一个 WPF UserControl。
在这个 UserControl 内部,我有几个 Button 可以在特定条件下使用,因此将它们绑定到命令是完美的。
这些按钮调用的命令不应该在用户控件之外可用。
如果我将我的命令设为私有,UserControl 的 XAML 表示他想要公共成员。
那么,怎样才能让一个 UserControl 内部有多个命令,但在 UserControl 之外不可用?
例子:
<Wizard CanGoPrevious="{Binding SomeViewModelProperty}">
<WizardPage>
<TextBlock>Page one</TextBlock>
</WizardPage>
</Wizard>
向导的 XAML:
<DockPanel DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type wizard:Wizard}}}" LastChildFill="True">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">
<Button Content="{Binding PreviousButtonText}" Command="{Binding GoToPreviousPageCommand}"/>
</StackPanel>
<ContentControl ></ContentControl>
</DockPanel>
背后的向导代码:
//Protected doesn't work. Also, this command should not be available outside of the Wizard `UserControl`
protected DelegateCommand GoToPreviousPageCommand { get; set; }
在构造函数中是这样赋值的
GoToPreviousPageCommand = new DelegateCommand(GoToPreviousPage, CanGoToPreviousPage);
private void GoToPreviousPage()
{
//[...]
}
private bool CanGoToNextPage()
{
//Some usage of the Wizard's DP:
return CanGoPrevious //&& some other stuff
}
【问题讨论】:
-
别打扰了。它是一个用户控件——在代码隐藏中包含与 UI 有关的逻辑非常好。
-
@Will 好的,但是找不到为 UserControl 提供对必须仅在内部可用的命令的公共访问权限。
标签: wpf xaml mvvm user-controls command