gpt4 book ai didi

WPF 用户控件 : Have a Command that is only available internally in the UserControl

转载 作者:行者123 更新时间:2023-12-03 10:17:35 34 4
gpt4 key购买 nike

我正在做一个 WPF UserControl .

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

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

如果我将我的命令设为私有(private),UserControl 的 XAML说他想要公共(public)成员。

那么,有什么方法可以让一个 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
}

最佳答案

编辑:添加示例代码(应该能够复制/过去/运行):
将命令保留为公开,但将您的 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>

以及在您的装配之外不可见的内部 View 模型:
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));
}
}

编辑:在关于如何使用 suer 控件和 View 模型的依赖属性的评论中提出了附加问题。方法有很多,这是其中之一:
        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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com