WPF:如何在视图中创建ViewModel

WPF:如何在视图中创建ViewModel,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,本质上,我有一个标记问题。我已经想出了一些解决方案,但我忍不住觉得这应该更简单一些。我不想让您走上我复杂的道路,而是想与您分享最简单的实现,并询问您将如何解决它 MainPage.xaml <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="6" /> <Co

本质上,我有一个标记问题。我已经想出了一些解决方案,但我忍不住觉得这应该更简单一些。我不想让您走上我复杂的道路,而是想与您分享最简单的实现,并询问您将如何解决它

MainPage.xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="6" />
        <ColumnDefinition />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="6" />
        <ColumnDefinition />
        <!--Additional Columns-->
    </Grid.ColumnDefinitions>
    <!--Row Definitions-->
    <Label Grid.Row="0" Grid.Column="0" Content="Vin:" HorizontalAlignment="Right" />
    <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2" Command="{Binding CreateVehicleCommand}" CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
    <Label Grid.Row="0" Grid.Column="3" Content="Manufacturer:" HorizontalAlignment="Right" />
    <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding Vehicle.Manufacturer, Mode=OneWay}" />
    <!--Additional Read Only Values-->
</Grid>
<ctrl:VMFactoryUserControl x:Class="GenericProject.View.GenericView"
                           x:TypeArguments="vm:GenericViewModel"
                           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                           xmlns:ctrl="clr-namespace:SomeProject.Controls;assembly=SomeProject.Controls"
                           xmlns:vm="clr-namespace:GenericProject.ViewModel">
    <Grid>
        <!-- Column Definitions -->
        <!-- Row Definitions -->
        <Label Grid.Row="0" Grid.Column="0" Content="Generic Id:" HorizontalAlignment="Right" />
        <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2"
                             Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                             CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
        <Label Grid.Row="0" Grid.Column="3" Content="Generic Property:" HorizontalAlignment="Right" />
        <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding GenericProperty, Mode=OneWay}" />
        <!--Additional Read Only Values-->
    </Grid>
</ctrl:VMFactoryUserControl>
<Page x:Class="GenericProject.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vw="clr-namespace:GenericProject.View">
    <StackPanel>
        <!-- Headers and Additional Content. -->
        <vw:EventView DataContext="{Binding Generic}"
                      VMFactory="{Binding DataContext.GenericFactory, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" />
    </StackPanel>
</Page>

在上面的示例中,如果创建车辆的命令在要创建的DataContext(车辆)之外,那么如何将网格的内容放入视图中

如果你想看看我的具体尝试,这个问题就在这里

如何将网格的内容放入给定的视图中 用于创建车辆的命令位于外部的约束 要创建的数据上下文(车辆)

这感觉像是一个竞争条件,而不是MVVM问题。我会先讨论这个问题,然后再提出第二个建议

  • 没有任何理由表明ViewModel不能包含另一个ViewModel作为引用,并且该引用是使用INotifyPropertyChanged机制绑定到的
  • 或者您的xaml(视图)页面包含对ViewModel的静态引用,该页面(视图)在其DataContext中不直接使用该引用,但某个控件无法绑定到包含控件的数据上下文之外的该静态引用
  • 无论哪种方式,都可以通过指向自身获取数据或提供另一个获取数据的管道来提供访问(在您提供的另一篇文章的回复中也提到了这一点)


    或者,您可以展平viewmodel以包含更多信息,并处理此IMHO竞争条件,这样就不会出现这种情况,控件以及网格可以以适当的格式访问信息


    我不能完全解决这个问题,因为您更清楚现在必须解决的设计目标和危险。

    我想出了一些办法,我对此比较满意。这使我免于创建100个复合ViewModel,虽然它确实引入了一些不必要的复杂性,但它确实大大减少了我需要编写的复制/粘贴代码的数量

    VMFactoryViewModel.cs

    public class CreatedViewModelEventArgs<T> : EventArgs where T : ViewModelBase
    {
        public T ViewModel { get; private set; }
    
        public CreatedViewModelEventArgs(T viewModel)
        {
            ViewModel = viewModel;
        }
    }
    
    public class VMFactoryViewModel<T> : ViewModelBase where T : ViewModelBase
    {
        private Func<string, T> _createViewModel;
        private RelayCommand<string> _createViewModelCommand;
        private readonly IDialogService _dialogService;
    
        /// <summary>
        /// Returns a command that creates the view model.
        /// </summary>
        public ICommand CreateViewModelCommand
        {
            get
            {
                if (_createViewModelCommand == null)
                    _createViewModelCommand = new RelayCommand<string>(x => CreateViewModel(x));
    
                return _createViewModelCommand;
            }
        }
    
        public event EventHandler<CreatedViewModelEventArgs<T>> CreatedViewModel;
    
        private void OnCreatedViewModel(T viewModel)
        {
            var handler = CreatedViewModel;
            if (handler != null)
                handler(this, new CreatedViewModelEventArgs<T>(viewModel));
        }
    
        public VMFactoryViewModel(IDialogService dialogService, Func<string, T> createViewModel)
        {
            _dialogService = dialogService;
            _createViewModel = createViewModel;
        }
    
        private void CreateViewModel(string viewModelId)
        {
            try
            {
                OnCreatedViewModel(_createViewModel(viewModelId));
            }
            catch (Exception ex)
            {
                _dialogService.Show(ex.Message);
            }
        }
    }
    
    public class VMFactoryUserControl<T> : UserControl where T : ViewModelBase
    {
        public static readonly DependencyProperty VMFactoryProperty = DependencyProperty.Register("VMFactory", typeof(VMFactoryViewModel<T>), typeof(VMFactoryUserControl<T>));
    
        public VMFactoryViewModel<T> VMFactory
        {
            get { return (VMFactoryViewModel<T>)GetValue(VMFactoryProperty); }
            set { SetValue(VMFactoryProperty, value); }
        }
    }
    
    public partial class GenericView : VMFactoryUserControl<GenericViewModel>
    {
        public GenericView()
        {
            InitializeComponent();
        }
    }
    
    public class MainPageViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private GenericViewModel _generic;
        private readonly VMFactoryViewModel<GenericViewModel> _genericFactory;
    
        public GenericViewModel Generic
        {
            get { return _generic; }
            private set
            {
                if (_generic != value)
                {
                    _generic = value;
                    base.OnPropertyChanged("Generic");
                } 
            }
        }
    
        public VMFactoryViewModel<GenericViewModel> GenericFactory
        {
            get { return _genericFactory; }
        }
    
        private void OnGenericFactoryCreatedViewModel(object sender, CreatedViewModelEventArgs<GenericViewModel> e)
        {
            Generic = e.ViewModel;
        }
    
        public MainPageViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
    
            _genericFactory = new VMFactoryViewModel<GenericViewModel>(_dialogService, x => new GenericViewModel(_dialogService, GetGeneric(x)));
            _genericFactory.CreatedViewModel += OnGenericFactoryCreatedViewModel;
        }
    
        private Generic GetGeneric(string genericId)
        {
            // Return some Generic model.
        }
    }
    
    public类CreatedViewModelEventArgs:EventArgs其中T:ViewModelBase
    {
    public T ViewModel{get;private set;}
    公共CreatedViewModelEventArgs(T视图模型)
    {
    ViewModel=ViewModel;
    }
    }
    公共类VMFactoryViewModel:ViewModelBase,其中T:ViewModelBase
    {
    private Funcu createViewModel;
    私有RelayCommand_createviewmodel命令;
    专用只读IDialogService(对话服务);
    /// 
    ///返回创建视图模型的命令。
    /// 
    公共ICommand createViewModel命令
    {
    收到
    {
    如果(_createViewModelCommand==null)
    _createViewModelCommand=new RelayCommand(x=>CreateViewModel(x));
    返回createViewModelCommand;
    }
    }
    公共事件事件处理程序CreatedViewModel;
    私有void OnCreatedViewModel(T viewModel)
    {
    var handler=CreatedViewModel;
    if(处理程序!=null)
    处理程序(这是新的CreatedViewModelEventArgs(viewModel));
    }
    公共VMFactoryViewModel(IDialogService对话框服务,Func createViewModel)
    {
    _dialogService=dialogService;
    _createViewModel=createViewModel;
    }
    私有void CreateViewModel(字符串viewModelId)
    {
    尝试
    {
    OnCreatedViewModel(_createViewModel(viewModelId));
    }
    捕获(例外情况除外)
    {
    _dialogService.Show(例如Message);
    }
    }
    }
    
    VMFactoryUserControl.cs

    public class CreatedViewModelEventArgs<T> : EventArgs where T : ViewModelBase
    {
        public T ViewModel { get; private set; }
    
        public CreatedViewModelEventArgs(T viewModel)
        {
            ViewModel = viewModel;
        }
    }
    
    public class VMFactoryViewModel<T> : ViewModelBase where T : ViewModelBase
    {
        private Func<string, T> _createViewModel;
        private RelayCommand<string> _createViewModelCommand;
        private readonly IDialogService _dialogService;
    
        /// <summary>
        /// Returns a command that creates the view model.
        /// </summary>
        public ICommand CreateViewModelCommand
        {
            get
            {
                if (_createViewModelCommand == null)
                    _createViewModelCommand = new RelayCommand<string>(x => CreateViewModel(x));
    
                return _createViewModelCommand;
            }
        }
    
        public event EventHandler<CreatedViewModelEventArgs<T>> CreatedViewModel;
    
        private void OnCreatedViewModel(T viewModel)
        {
            var handler = CreatedViewModel;
            if (handler != null)
                handler(this, new CreatedViewModelEventArgs<T>(viewModel));
        }
    
        public VMFactoryViewModel(IDialogService dialogService, Func<string, T> createViewModel)
        {
            _dialogService = dialogService;
            _createViewModel = createViewModel;
        }
    
        private void CreateViewModel(string viewModelId)
        {
            try
            {
                OnCreatedViewModel(_createViewModel(viewModelId));
            }
            catch (Exception ex)
            {
                _dialogService.Show(ex.Message);
            }
        }
    }
    
    public class VMFactoryUserControl<T> : UserControl where T : ViewModelBase
    {
        public static readonly DependencyProperty VMFactoryProperty = DependencyProperty.Register("VMFactory", typeof(VMFactoryViewModel<T>), typeof(VMFactoryUserControl<T>));
    
        public VMFactoryViewModel<T> VMFactory
        {
            get { return (VMFactoryViewModel<T>)GetValue(VMFactoryProperty); }
            set { SetValue(VMFactoryProperty, value); }
        }
    }
    
    public partial class GenericView : VMFactoryUserControl<GenericViewModel>
    {
        public GenericView()
        {
            InitializeComponent();
        }
    }
    
    public class MainPageViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private GenericViewModel _generic;
        private readonly VMFactoryViewModel<GenericViewModel> _genericFactory;
    
        public GenericViewModel Generic
        {
            get { return _generic; }
            private set
            {
                if (_generic != value)
                {
                    _generic = value;
                    base.OnPropertyChanged("Generic");
                } 
            }
        }
    
        public VMFactoryViewModel<GenericViewModel> GenericFactory
        {
            get { return _genericFactory; }
        }
    
        private void OnGenericFactoryCreatedViewModel(object sender, CreatedViewModelEventArgs<GenericViewModel> e)
        {
            Generic = e.ViewModel;
        }
    
        public MainPageViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
    
            _genericFactory = new VMFactoryViewModel<GenericViewModel>(_dialogService, x => new GenericViewModel(_dialogService, GetGeneric(x)));
            _genericFactory.CreatedViewModel += OnGenericFactoryCreatedViewModel;
        }
    
        private Generic GetGeneric(string genericId)
        {
            // Return some Generic model.
        }
    }
    
    公共类VMFactoryUserControl:UserControl其中T:ViewModelBase
    {
    公共静态只读DependencyProperty VMFactoryProperty=DependencyProperty.Register(“VMFactory”,typeof(VMFactoryViewModel),typeof(VMFactoryUserControl));
    公共VMFactoryViewModel VMFactory
    {
    获取{return(VMFactoryViewModel)GetValue(VMFactoryProperty);}
    set{SetValue(VMFactoryProperty,value);}
    }
    }
    
    GenericView.xaml

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="6" />
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="6" />
            <ColumnDefinition />
            <!--Additional Columns-->
        </Grid.ColumnDefinitions>
        <!--Row Definitions-->
        <Label Grid.Row="0" Grid.Column="0" Content="Vin:" HorizontalAlignment="Right" />
        <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2" Command="{Binding CreateVehicleCommand}" CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
        <Label Grid.Row="0" Grid.Column="3" Content="Manufacturer:" HorizontalAlignment="Right" />
        <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding Vehicle.Manufacturer, Mode=OneWay}" />
        <!--Additional Read Only Values-->
    </Grid>
    
    <ctrl:VMFactoryUserControl x:Class="GenericProject.View.GenericView"
                               x:TypeArguments="vm:GenericViewModel"
                               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                               xmlns:ctrl="clr-namespace:SomeProject.Controls;assembly=SomeProject.Controls"
                               xmlns:vm="clr-namespace:GenericProject.ViewModel">
        <Grid>
            <!-- Column Definitions -->
            <!-- Row Definitions -->
            <Label Grid.Row="0" Grid.Column="0" Content="Generic Id:" HorizontalAlignment="Right" />
            <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2"
                                 Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                 CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
            <Label Grid.Row="0" Grid.Column="3" Content="Generic Property:" HorizontalAlignment="Right" />
            <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding GenericProperty, Mode=OneWay}" />
            <!--Additional Read Only Values-->
        </Grid>
    </ctrl:VMFactoryUserControl>
    
    <Page x:Class="GenericProject.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vw="clr-namespace:GenericProject.View">
        <StackPanel>
            <!-- Headers and Additional Content. -->
            <vw:EventView DataContext="{Binding Generic}"
                          VMFactory="{Binding DataContext.GenericFactory, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" />
        </StackPanel>
    </Page>
    
    
    
    GenericView.xaml.cs

    public class CreatedViewModelEventArgs<T> : EventArgs where T : ViewModelBase
    {
        public T ViewModel { get; private set; }
    
        public CreatedViewModelEventArgs(T viewModel)
        {
            ViewModel = viewModel;
        }
    }
    
    public class VMFactoryViewModel<T> : ViewModelBase where T : ViewModelBase
    {
        private Func<string, T> _createViewModel;
        private RelayCommand<string> _createViewModelCommand;
        private readonly IDialogService _dialogService;
    
        /// <summary>
        /// Returns a command that creates the view model.
        /// </summary>
        public ICommand CreateViewModelCommand
        {
            get
            {
                if (_createViewModelCommand == null)
                    _createViewModelCommand = new RelayCommand<string>(x => CreateViewModel(x));
    
                return _createViewModelCommand;
            }
        }
    
        public event EventHandler<CreatedViewModelEventArgs<T>> CreatedViewModel;
    
        private void OnCreatedViewModel(T viewModel)
        {
            var handler = CreatedViewModel;
            if (handler != null)
                handler(this, new CreatedViewModelEventArgs<T>(viewModel));
        }
    
        public VMFactoryViewModel(IDialogService dialogService, Func<string, T> createViewModel)
        {
            _dialogService = dialogService;
            _createViewModel = createViewModel;
        }
    
        private void CreateViewModel(string viewModelId)
        {
            try
            {
                OnCreatedViewModel(_createViewModel(viewModelId));
            }
            catch (Exception ex)
            {
                _dialogService.Show(ex.Message);
            }
        }
    }
    
    public class VMFactoryUserControl<T> : UserControl where T : ViewModelBase
    {
        public static readonly DependencyProperty VMFactoryProperty = DependencyProperty.Register("VMFactory", typeof(VMFactoryViewModel<T>), typeof(VMFactoryUserControl<T>));
    
        public VMFactoryViewModel<T> VMFactory
        {
            get { return (VMFactoryViewModel<T>)GetValue(VMFactoryProperty); }
            set { SetValue(VMFactoryProperty, value); }
        }
    }
    
    public partial class GenericView : VMFactoryUserControl<GenericViewModel>
    {
        public GenericView()
        {
            InitializeComponent();
        }
    }
    
    public class MainPageViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private GenericViewModel _generic;
        private readonly VMFactoryViewModel<GenericViewModel> _genericFactory;
    
        public GenericViewModel Generic
        {
            get { return _generic; }
            private set
            {
                if (_generic != value)
                {
                    _generic = value;
                    base.OnPropertyChanged("Generic");
                } 
            }
        }
    
        public VMFactoryViewModel<GenericViewModel> GenericFactory
        {
            get { return _genericFactory; }
        }
    
        private void OnGenericFactoryCreatedViewModel(object sender, CreatedViewModelEventArgs<GenericViewModel> e)
        {
            Generic = e.ViewModel;
        }
    
        public MainPageViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
    
            _genericFactory = new VMFactoryViewModel<GenericViewModel>(_dialogService, x => new GenericViewModel(_dialogService, GetGeneric(x)));
            _genericFactory.CreatedViewModel += OnGenericFactoryCreatedViewModel;
        }
    
        private Generic GetGeneric(string genericId)
        {
            // Return some Generic model.
        }
    }
    
    公共部分类GenericView:VMFactoryUserControl
    {
    公共通用视图()
    {
    初始化组件();
    }
    }
    
    MainPageViewModel.cs

    public class CreatedViewModelEventArgs<T> : EventArgs where T : ViewModelBase
    {
        public T ViewModel { get; private set; }
    
        public CreatedViewModelEventArgs(T viewModel)
        {
            ViewModel = viewModel;
        }
    }
    
    public class VMFactoryViewModel<T> : ViewModelBase where T : ViewModelBase
    {
        private Func<string, T> _createViewModel;
        private RelayCommand<string> _createViewModelCommand;
        private readonly IDialogService _dialogService;
    
        /// <summary>
        /// Returns a command that creates the view model.
        /// </summary>
        public ICommand CreateViewModelCommand
        {
            get
            {
                if (_createViewModelCommand == null)
                    _createViewModelCommand = new RelayCommand<string>(x => CreateViewModel(x));
    
                return _createViewModelCommand;
            }
        }
    
        public event EventHandler<CreatedViewModelEventArgs<T>> CreatedViewModel;
    
        private void OnCreatedViewModel(T viewModel)
        {
            var handler = CreatedViewModel;
            if (handler != null)
                handler(this, new CreatedViewModelEventArgs<T>(viewModel));
        }
    
        public VMFactoryViewModel(IDialogService dialogService, Func<string, T> createViewModel)
        {
            _dialogService = dialogService;
            _createViewModel = createViewModel;
        }
    
        private void CreateViewModel(string viewModelId)
        {
            try
            {
                OnCreatedViewModel(_createViewModel(viewModelId));
            }
            catch (Exception ex)
            {
                _dialogService.Show(ex.Message);
            }
        }
    }
    
    public class VMFactoryUserControl<T> : UserControl where T : ViewModelBase
    {
        public static readonly DependencyProperty VMFactoryProperty = DependencyProperty.Register("VMFactory", typeof(VMFactoryViewModel<T>), typeof(VMFactoryUserControl<T>));
    
        public VMFactoryViewModel<T> VMFactory
        {
            get { return (VMFactoryViewModel<T>)GetValue(VMFactoryProperty); }
            set { SetValue(VMFactoryProperty, value); }
        }
    }
    
    public partial class GenericView : VMFactoryUserControl<GenericViewModel>
    {
        public GenericView()
        {
            InitializeComponent();
        }
    }
    
    public class MainPageViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private GenericViewModel _generic;
        private readonly VMFactoryViewModel<GenericViewModel> _genericFactory;
    
        public GenericViewModel Generic
        {
            get { return _generic; }
            private set
            {
                if (_generic != value)
                {
                    _generic = value;
                    base.OnPropertyChanged("Generic");
                } 
            }
        }
    
        public VMFactoryViewModel<GenericViewModel> GenericFactory
        {
            get { return _genericFactory; }
        }
    
        private void OnGenericFactoryCreatedViewModel(object sender, CreatedViewModelEventArgs<GenericViewModel> e)
        {
            Generic = e.ViewModel;
        }
    
        public MainPageViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
    
            _genericFactory = new VMFactoryViewModel<GenericViewModel>(_dialogService, x => new GenericViewModel(_dialogService, GetGeneric(x)));
            _genericFactory.CreatedViewModel += OnGenericFactoryCreatedViewModel;
        }
    
        private Generic GetGeneric(string genericId)
        {
            // Return some Generic model.
        }
    }
    
    public类MainPageViewModel:ViewModelBase
    {
    专用只读IDialogService(对话服务);
    私有通用视图模型_generic;
    私有只读VMFactoryViewModel\u genericFactory;
    公共通用视图模型通用
    {
    获取{return\u generic;}
    专用设备
    {
    如果(_generic!=值)
    {
    _通用=值;
    基于财产变更(“通用”);
    } 
    }
    }
    公共VMFactoryViewModel通用工厂
    {
    获取{return\u genericFactory;}
    }
    private void OnGenericFactoryCreatedViewModel(对象发送方,CreatedViewModelEventArgs e)
    {
    通用=e.ViewModel;
    }
    公共主页视图模型(IDialogService dialogService)
    {
    _dialogService=dialogService;
    _genericFactory=new-VMFactoryViewModel(_-dialogService,x=>new-GenericViewModel(_-dialogService,GetGeneric(x));
    _genericFactory.CreatedViewModel+=OnGenericFactoryCreatedViewModel;
    }
    私有泛型GetGeneric(字符串genericId)
    {
    //返回一些通用模型。
    }
    }
    
    MainPage.xaml

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="6" />
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="6" />
            <ColumnDefinition />
            <!--Additional Columns-->
        </Grid.ColumnDefinitions>
        <!--Row Definitions-->
        <Label Grid.Row="0" Grid.Column="0" Content="Vin:" HorizontalAlignment="Right" />
        <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2" Command="{Binding CreateVehicleCommand}" CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
        <Label Grid.Row="0" Grid.Column="3" Content="Manufacturer:" HorizontalAlignment="Right" />
        <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding Vehicle.Manufacturer, Mode=OneWay}" />
        <!--Additional Read Only Values-->
    </Grid>
    
    <ctrl:VMFactoryUserControl x:Class="GenericProject.View.GenericView"
                               x:TypeArguments="vm:GenericViewModel"
                               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                               xmlns:ctrl="clr-namespace:SomeProject.Controls;assembly=SomeProject.Controls"
                               xmlns:vm="clr-namespace:GenericProject.ViewModel">
        <Grid>
            <!-- Column Definitions -->
            <!-- Row Definitions -->
            <Label Grid.Row="0" Grid.Column="0" Content="Generic Id:" HorizontalAlignment="Right" />
            <ctrl:CommandTextBox Grid.Row="0" Grid.Column="2"
                                 Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                 CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" />
            <Label Grid.Row="0" Grid.Column="3" Content="Generic Property:" HorizontalAlignment="Right" />
            <TextBox Grid.Row="0" Grid.Column="5" IsEnabled="False" Text="{Binding GenericProperty, Mode=OneWay}" />
            <!--Additional Read Only Values-->
        </Grid>
    </ctrl:VMFactoryUserControl>
    
    <Page x:Class="GenericProject.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vw="clr-namespace:GenericProject.View">
        <StackPanel>
            <!-- Headers and Additional Content. -->
            <vw:EventView DataContext="{Binding Generic}"
                          VMFactory="{Binding DataContext.GenericFactory, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" />
        </StackPanel>
    </Page>
    

    我看不出你在哪里挣扎。为什么不能将模型注入视图模型?为什么这会打破文本框?如果改变V