c# - 如何从Windows 8.1 Store应用程序中的ViewModel C#类将按钮插入网格

标签 c# mvvm windows-store-apps winrt-xaml

我可以从文件后面的代码中将按钮插入网格控件

var fElem = new Button();                 

fElem.Content = "button text";
bgGrid.Children.Add(fElem); // bgGrid defined in xaml

如何从 View 模型类向网格添加相同的按钮?

最佳答案

在xaml页面(xaml.cs)的代码背后,您需要公开显示网格

public Grid MyGrid => bgGrid;

然后,您可以从ViewModel中访问它,如下所示:
Frame rootFrame = Window.Current.Content as Frame;
var page = rootFrame.Content as YourPageClassName;
var fElem = new Button();                 
fElem.Content = "button text";
page.MyGrid.Children.Add(fElem); 

但是,这根本不是MVVM。

关于c# - 如何从Windows 8.1 Store应用程序中的ViewModel C#类将按钮插入网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062670/

相关文章:

c# - 如何读取 Windows 应用商店应用程序中的二进制文件?

c# - 如何在 ASP.NET Core 中获取 Active Directory 当前用户显示名称?

c# - 从任务体内取消任务

wpf - MVVM 模式 - 执行 View 操作

c# - 如何在 WPF ViewModel 中使用模型验证规则

c# - 语言选择 : C++ or C# for Windows Store app?

c# - 为什么不能继承/分层强制执行泛型类型约束

c# - 编写具有远程 shell 功能的程序,如 netcat

c# - 为什么 RelayCommand<T>.Execute 采用对象而不是 T?

c# - 在 WinRT 中将视频上传到 Youtube