gpt4 book ai didi

wpf - MVVM 主窗口控件从子用户控件绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 03:57:17 27 4
gpt4 key购买 nike

我是 MVVM 的新手,现在正在完成我的第一个 POC。但是,为了解决一个问题 2 天,我一直在努力解决问题。向你们解释的想法可能会帮助并如此迅速地解决问题。现在让我简要介绍一下我的问题。我有一个 WPF MVVM 应用程序,其中一个主视图绑定(bind)到 MainViewModel。我在此处使用 Textblock 来绑定(bind) View 模型中的一些内容,同时加载运行良好的屏幕。我还将 ChildUserControl 绑定(bind)到 ChildViewModel。现在,我需要将不同的内容绑定(bind)到主窗口中的文本 block ,从用户控件到用户控件级别发生的某些操作。怎么可能?

这是我的示例代码主窗口.Xaml

<Window.Resources>
<viewModel:MainViewModel x:Key="mainWindowViewModel"/></Window.Resources>

<TextBlock Name="txtStatus" Text="{Binding StatusMessage, Mode=OneWay }"/>

子用户控件.xaml

<UserControl.Resources>
<viewModel:ChildModelView x:Key="ChildModelView"/> </UserControl.Resources>

public class ChildModelView : BaseViewModel
{
// Some child level logic..
// then need to update the txtStatus text block from parent
}

非常感谢您的帮助..!

最佳答案

实现此目的的一种简单方法是使用 IoC。创建 subview 模型时,将主视图模型的引用传递给 subview 模型,并将其作为私有(private)只读变量保存。您现在可以访问所有主要 VM 公开。

另一种解决方案可能是使用中介者模式。

搭建了一个简单的应用程序来演示 IoC 解决方案。

App.xaml.cs

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

var window = new MainWindow() {DataContext = new MainWindowViewModel() };
window.Show();
}
}

MainWindowViewModel.cs

public class MainWindowViewModel : ViewModelBase
{
private string _statusMessage;

public string StatusMessage
{
get { return _statusMessage; }
set { _statusMessage = value; this.OnPropertyChanged("StatusMessage"); }
}

public ICommand OpenChildCommand { get; private set; }

public MainWindowViewModel()
{
this.StatusMessage = "No status";
this.OpenChildCommand = new DelegateCommand((o) => this.OpenChild());
}

private void OpenChild()
{
var window = new ChildWindow {DataContext = new ChildWindowViewModel(this)};
window.Show();
}
}

ChildWindowViewModel.cs

public class ChildWindowViewModel : ViewModelBase
{
private readonly MainWindowViewModel _mainvm;

public ChildWindowViewModel(MainWindowViewModel mainvm)
{
_mainvm = mainvm;
this.UpdateStatusCommand = new DelegateCommand((o) => this.UpdateStatus());
}

public ICommand UpdateStatusCommand { get; private set; }

private void UpdateStatus()
{
this._mainvm.StatusMessage = "New Status";
}
}

ViewModelBase.cs

public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
}

DelegateCommand.cs

public class DelegateCommand : ICommand
{
/// <summary>
/// Action to be performed when this command is executed
/// </summary>
private Action<object> executionAction;

/// <summary>
/// Predicate to determine if the command is valid for execution
/// </summary>
private Predicate<object> canExecutePredicate;

/// <summary>
/// Initializes a new instance of the DelegateCommand class.
/// The command will always be valid for execution.
/// </summary>
/// <param name="execute">The delegate to call on execution</param>
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}

/// <summary>
/// Initializes a new instance of the DelegateCommand class.
/// </summary>
/// <param name="execute">The delegate to call on execution</param>
/// <param name="canExecute">The predicate to determine if command is valid for execution</param>
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}

this.executionAction = execute;
this.canExecutePredicate = canExecute;
}

/// <summary>
/// Raised when CanExecute is changed
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

/// <summary>
/// Executes the delegate backing this DelegateCommand
/// </summary>
/// <param name="parameter">parameter to pass to predicate</param>
/// <returns>True if command is valid for execution</returns>
public bool CanExecute(object parameter)
{
return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter);
}

/// <summary>
/// Executes the delegate backing this DelegateCommand
/// </summary>
/// <param name="parameter">parameter to pass to delegate</param>
/// <exception cref="InvalidOperationException">Thrown if CanExecute returns false</exception>
public void Execute(object parameter)
{
if (!this.CanExecute(parameter))
{
throw new InvalidOperationException("The command is not valid for execution, check the CanExecute method before attempting to execute.");
}
this.executionAction(parameter);
}
}

主窗口.xaml

<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding StatusMessage, Mode=OneWay}" />
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Open Child Window"
Command="{Binding Path=OpenChildCommand}"/>
</StackPanel>

子窗口.xaml

<Window x:Class="WpfApplication2.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ChildWindow" Height="300" Width="300">
<Grid>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="UpdateStatus"
Command="{Binding Path=UpdateStatusCommand}" />
</Grid>

点击更新状态前的图片

Before

点击updatestatus后的图片

enter image description here

关于wpf - MVVM 主窗口控件从子用户控件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418671/

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