作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个 WPF UserControl
.
在这个 UserControl 内部,我有几个 Button
可以在某些条件下使用,因此将它们绑定(bind)到命令是完美的。
这些按钮调用的命令不应该在用户控件之外可用。
如果我将我的命令设为私有(private),UserControl
的 XAML说他想要公共(public)成员。
那么,有什么方法可以让一个 UserControl 在内部具有多个命令,但在 UserControl 之外不可用?
例子:
<Wizard CanGoPrevious="{Binding SomeViewModelProperty}">
<WizardPage>
<TextBlock>Page one</TextBlock>
</WizardPage>
</Wizard>
<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
}
最佳答案
编辑:添加示例代码(应该能够复制/过去/运行):
将命令保留为公开,但将您的 ViewModel 设为内部将使命令对外部代码不可见!
<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));
}
}
public string MySampleDependencyProperty
{
get { return (string)GetValue(MySampleDependencyPropertyProperty); }
set { SetValue(MySampleDependencyPropertyProperty, value); }
}
public static readonly DependencyProperty MySampleDependencyPropertyProperty =
DependencyProperty.Register("MySampleDependencyProperty", typeof(string),
typeof(MyUserControl),
new FrameworkPropertyMetadata("",
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, e) => ((MyUserControl)o).OnMySampleDependencyPropertyChanged()));
private void OnMySampleDependencyPropertyChanged()
{
viewMdoel.WhateverProperty = MySampleDependencyProperty;
}
关于WPF 用户控件 : Have a Command that is only available internally in the UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32397334/
我是一名优秀的程序员,十分优秀!