我如何从另一个 class C# mvvm 访问我的 ObservableCollection 以将项目添加到 collection
How can i acces my ObservableCollection from another class C# mvvm to add Items to that collection
这是我第一个 class 从我制作标签的地方我想将我的标签添加到 ObservableCollection
namespace Notepad__.ViewModel
{
public class TabViewModel
{
public DocumentModel m_doc;
public ObservableCollection<TabItem> Tabs { get; set; }
public TabViewModel()
{
Tabs = new ObservableCollection<TabItem>();
Tabs.Add(new TabItem { Header = "s", Content = "One's content" });
Tabs.Add(new TabItem { Header = "Two", Content = "Two's content" });
}
public class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
}
}
}
这是我要访问 Collection 以添加新选项卡的 class。创建新文件后,我想将该文件添加到 collection 和我的选项卡控件以更新选项卡项
namespace Notepad__.ViewModel
{
public class FileModel
{
public DocumentModel Document { get; private set; }
public ICommand NewCommand { get; }
public ICommand SaveCommand { get; }
public ICommand OpenCommand { get; }
public ICommand SaveAsCommand { get; }
public FileModel(DocumentModel document)
{
Document = document;
NewCommand = new RelayCommand(NewFile);
SaveCommand = new RelayCommand(SaveFile);
SaveAsCommand = new RelayCommand(SaveFileAs);
OpenCommand = new RelayCommand(OpenFile);
}
public void NewFile()
{
Document.FileName = "New File";
Document.FilePath = string.Empty;
Document.Text = string.Empty;
//TabViewModel.TabItem("")
}
How can i acces my ObservableCollection from another class C#
使集合静态化。
public static ObservableCollection<TabItem> Tabs { get; set; }
然后就这样访问
TabViewModel.Tabs.Add(...);
请注意,针对这种情况的 WPF 首选项是在 app
上创建视图模型引用作为 static,
,然后由主页和其他页面引用它。比如App.VM.Tabs.Add()
.
这是我第一个 class 从我制作标签的地方我想将我的标签添加到 ObservableCollection
namespace Notepad__.ViewModel
{
public class TabViewModel
{
public DocumentModel m_doc;
public ObservableCollection<TabItem> Tabs { get; set; }
public TabViewModel()
{
Tabs = new ObservableCollection<TabItem>();
Tabs.Add(new TabItem { Header = "s", Content = "One's content" });
Tabs.Add(new TabItem { Header = "Two", Content = "Two's content" });
}
public class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
}
}
}
这是我要访问 Collection 以添加新选项卡的 class。创建新文件后,我想将该文件添加到 collection 和我的选项卡控件以更新选项卡项
namespace Notepad__.ViewModel
{
public class FileModel
{
public DocumentModel Document { get; private set; }
public ICommand NewCommand { get; }
public ICommand SaveCommand { get; }
public ICommand OpenCommand { get; }
public ICommand SaveAsCommand { get; }
public FileModel(DocumentModel document)
{
Document = document;
NewCommand = new RelayCommand(NewFile);
SaveCommand = new RelayCommand(SaveFile);
SaveAsCommand = new RelayCommand(SaveFileAs);
OpenCommand = new RelayCommand(OpenFile);
}
public void NewFile()
{
Document.FileName = "New File";
Document.FilePath = string.Empty;
Document.Text = string.Empty;
//TabViewModel.TabItem("")
}
How can i acces my ObservableCollection from another class C#
使集合静态化。
public static ObservableCollection<TabItem> Tabs { get; set; }
然后就这样访问
TabViewModel.Tabs.Add(...);
请注意,针对这种情况的 WPF 首选项是在 app
上创建视图模型引用作为 static,
,然后由主页和其他页面引用它。比如App.VM.Tabs.Add()
.