如何在Window.Resources标记中使用带有数据绑定的视图模型、模型和命令类



如果WPF MVVM应该没有代码隐藏,为什么在使用ICommand时,需要在Window.xaml.cs代码隐藏中实例化DataContext属性?我观看并关注了YouTube WPF MVVM、数据绑定、ICommand、INotifyPropertyChanged视频,我很困惑。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel.VM_CalcRblAmt();
    }
}

如何使用具有数据绑定的视图模型、模型和命令类在Window.Resource标记中?如果这是正确的mvvm模式?下面的Window.Resource标记中有类似的内容(但不起作用)?我之所以这么问,是因为在不同作者的WPF MVVM教程中,我看不到如何将视图模型、模型和命令类插入UI中的xml命名空间(本例中为Window)。例如:

<Window...
         xmlns:nsviewmodel="clr-namespace:Wpf_MVVVM_Rbr.ViewModel" />
<Window.Resources>
<nsrbrvm:RbrCoreViewModel x:Key="objRbrTemp"
  <Button x:Name="btnRebalancer" 
     Command="{Binding CalcRbrAmtCommand}" 
     Content="Rebalance"/>
  <TextBox x:Name="txtAmtDaily" Text="{Binding model_Rblr_Temp.AmtDaily, UpdateSourceTrigger=PropertyChanged}" />
</Window.Resources>

同样,上面的代码实际上并不在Window.Resources之间标记。它在Window.Resources标记之外的UI XAML代码中。当前是否运行。但是MVVM模式在Window.Resources或Page.Resouries标记中不需要它吗?这些标记引用xmlns命名空间并给它一个x:Key="someKeyName",并在Resources标记中使用数据绑定?

因此,如果是正确的MVVM实践,我如何使用数据绑定将Window.Resources数据绑定、视图模型、命令和模型类放在一起?

提前感谢!真诚地Me

1。如果WPF MVVM应该没有代码隐藏,为什么在使用ICommand时,需要在Window.xaml.cs代码隐藏中实例化DataContext属性有许多方法可以设置DataContext:

第一种方法视图:

<Window.DataContext>
   <local:MainWindowViewModel/>
</Window.DataContext>

第二种方法您应该覆盖App.xaml.cs 的OnStartUp()方法

public partial class App : Application
{
     protected override void OnStartup(StartupEventArgs e)
    {
            base.OnStartup(e);
            MainWindow app = new MainWindow();
            ProductViewModel context = new ProductViewModel();
            app.DataContext = context;
            app.Show();
     }
}

第三种方法在Windows的构造函数中:

public partial class MainWindow : Window
{
     public MainWindow()
     {
          InitializeComponent();
          DataContext=new MainWindowViewModel();
     }
}

第四种方法可以通过UnityContainer的DependencyInjection设置DataContext。但DependencyInjection、Prism和UnityContainer是其他问题,超出了这个问题的范围。举个例子:

protected override void RegisterTypes()
{ 
    unityContainer.RegisterType<object, ItemControl>("ModuleAUpper");
    unityContainer.RegisterType<IViewModelItemControl, ViewModelItemControl>();
    unityContainer.RegisterTypeForNavigation<ItemControl>();
}

2.如何在Window.Resource标记中使用具有数据绑定的视图模型、模型、命令类?如果这是正确的mvvm模式?下面的Window.Resource标记中有类似的内容(但不起作用)

资源只是存储数据的机会。要设置放置在viewModel中的属性和XAML之间的绑定,您应该:

private string message="helloWorld";    
public string Message
{
    get { return message;}
    set { message = value;}
}

和XAML:

MVVM:范围内绑定Button

<Button Command="{Binding ClickCommand}" Width="100" Height="100" Content="wefwfwef"/>

和ViewModel:

private ICommand _clickCommand;
public ICommand ClickCommand
{
   get
   {
     return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute));
   }
}
private bool _canExecute;
public class CommandHandler : ICommand
{
  private Action _action;
  private bool _canExecute;
  public CommandHandler(Action action, bool canExecute)
  {
      _action = action;
      _canExecute = canExecute;
  }
  public bool CanExecute(object parameter)
  {
     return _canExecute;
  }
  public event EventHandler CanExecuteChanged;
  public void Execute(object parameter)
  {
     _action();
  }
}

更新:

我建议你阅读我读过的最好的MVVM教程Rachel Lim。

谢谢,StepUp!

你答案的第一部分我得了第一名和第二名!I我正在使用第一种方法来避免任何代码落后。第三个我确信是有效的,只是也许我错过了一些你以为我知道的东西。但方法一是我一直在寻找的!谢谢当我熟悉这个类时,我会花一些时间尝试使用和理解方法4,UnityContainer.Registry类型。感谢你的第二部分回答,问题,我还没有逆向工程和应用。有了ICommand接口,我一直在使用ICommand和我从youTube教程视频中获得的内容。但一旦我使用了你的代码,我会回到这里感谢你。

最新更新