C# 初始化窗口视图后,程序生成数百万个绑定实例
我编程应用程序使用文本模板 我使用Visual Studio 2017。NET框架4.6.2。WPF和MVVM模式 因此,当我为视图创建实例时,程序会生成大量带有绑定或其他内容的实例 创建和显示视图的我的命令代码:C# 初始化窗口视图后,程序生成数百万个绑定实例,c#,wpf,mvvm,C#,Wpf,Mvvm,我编程应用程序使用文本模板 我使用Visual Studio 2017。NET框架4.6.2。WPF和MVVM模式 因此,当我为视图创建实例时,程序会生成大量带有绑定或其他内容的实例 创建和显示视图的我的命令代码: public RelayCommand AddTemplate => addTemplate ?? (addTemplate = new RelayCommand(obj =>{ TemplateAddOrEditView templateAddView = new Te
public RelayCommand AddTemplate => addTemplate ??
(addTemplate = new RelayCommand(obj =>{
TemplateAddOrEditView templateAddView = new TemplateAddOrEditView(){
Owner = Application.Current.MainWindow};
TemplateAddOrEditViewModel templateAddViewModel = new
TemplateAddOrEditViewModel(SelectedGroup, templateAddView);
templateAddView.DataContext = templateAddViewModel;
templateAddView.Show();}));
XAML中按钮的名称和代码
<Button Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Content="Новый шаблон" Margin="3,0,3,3" FontSize="14" Background="White" BorderBrush="Black" Command="{Binding AddTemplate}"></Button>
[MainWindow of App][1][Add Group for Template][2][Snapshot when i press button][3][1]:[2]:[3]:这里相关的是
TemplateAddOrEditView的源代码(包括xaml)。公共部分类TemplateAddOrEditView:Window{public TemplateAddOrEditView(){InitializeComponent();DataContext=new TemplateAddOrEditView();}}}
XAMLIn构造函数,您提供的构造函数可以执行以下操作:DataContext=new TemplateAddOrEditView()代码>。这意味着您将在TemplateAddOrEditView()
自身的构造函数中创建TemplateAddOrEditView()的新实例,这将导致递归创建新窗口。您的意思可能是DataContext=newtemplateaddoreditview**Model**()