WPF UserControls:具有仅在UserControl内部可用的命令

时间:2015-09-04 11:48:22

标签: wpf xaml mvvm user-controls command

我正在做一个WPF UserControl

在这个UserControl的内部,我可以在某些条件下使用几个Button,因此将它们绑定到命令是完美的。

这些按钮调用的命令不应该在UserControl之外可用。

如果我将我的命令设为私有,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
    }

1 个答案:

答案 0 :(得分:1)

编辑:添加示例代码(应该能够复制/过去/运行): 将命令保留为public,但使您的ViewModel内部将使命令对外部代码不可见!

@Table(name = "card")
@Entity
public class Card {

    @Column(name = "id")
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToMany(mappedBy = "menu")
    private Set<Menu> menus;
....
}

@Table(name = "menu")
@Entity
public class Menu {
    @Column(name = "id")
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "menu_card", joinColumns = {@JoinColumn(name = "menu")},
            inverseJoinColumns = {@JoinColumn(name = "card")})
    private Set<Card> cards;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_menu")
    private Menu parentMenu;
    }
}

测试用户控件的窗口后面的代码:

<Window x:Class="InternalCommandUsageSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:InternalCommandUsageSample"
    Title="MainWindow" Height="350" Width="525">
<local:MyUserControl/>

}

用户控制:

using System.Windows;

namespace InternalCommandUsageSample
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        var vm = new MyViewModel();
        DataContext = vm;

        InitializeComponent();
    }
}

以及在程序集外部不可见的内部视图模型:

<UserControl x:Class="InternalCommandUsageSample.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
    <TextBlock Text="{Binding Message, Mode=OneWay}"/>
    <Button Content="Test Me" Command="{Binding TestMeCommand}"/>
</StackPanel>

编辑:在评论中询问了如何使用超级控件和视图模型的依赖属性的附加问题。有很多方法,这是其中之一:

internal class MyViewModel : INotifyPropertyChanged
{
    private string _message = "click the button";
    private DelegateCommand _cmd;

    public DelegateCommand TestMeCommand
    {
        get
        {
            return _cmd ?? (_cmd = new DelegateCommand(cmd => { Message = "Your button click envoked an internal command"; }));
        }
    }

    public string Message
    {
        get { return _message; }
        set
        {
            if (_message != value)
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}