WPF 使用绑定将网格行设置为动态 XAML 对象

发布于 2024-10-19 02:22:04 字数 885 浏览 1 评论 0原文

我希望将 Grid 行(不是 DataGrid)的内容绑定到给定的 DataContext 属性。换句话说,我希望出现在网格的第 0 行中的 UserControl 在 DataContext 属性 XX 从 UserControl 更改为另一个 UserControl 时发生更改。知道怎么做吗?

我想要实现的是一个松散耦合的 UI,由带有占位符的 Shell(主窗口)组成,但能够允许从任何占位符更改此类占位符的内容,如何?好吧,将一些占位符控件的命令绑定到 Shell DataContext 的命令(ShellViewModel),即按层次结构将任何占位符的所有控件的 DataContext 不覆盖 DataContext 属性,然后在此类命令的实现中更改 XX。

更具体地说,我希望我的应用程序的导航控件 (UI) 可以轻松更改,而不影响导航的语义,而我的新手在阅读 MVVM 后唯一想到的就是上述方法。你有更好的主意吗?这是正确的还是太松散了;)

我在第一个回复后添加了这部分:

数据模板不符合要求。为什么?嗯,很简单,使用数据模板,Shell 必须首先知道组成应用程序 UI 的 UI 元素的所有可能部分(通常采用 UserControls 形式的视图),这里的想法是避免这种情况。这个想法是拥有一个带有占位符的 Shell,并且 Shell 通过其 DataContext(Shell 的 ViewModel)向任何将填充此类占位符的前端未知 UI 组件公开命令。此类命令可以被视为一种用于与导航系统交互的 API/接口。

经过更多研究后,我得到了一个想法,所以一个答案可以是如下所示:

<ContentControl Grid.Row="0">
    <Binding Path="CurrentView"/>
 </ContentControl>

I want have the content of a Grid's row (not DataGrid) bound to a given DataContext property. In others words, I want that the UserControl that appear in, lets say... row 0 of my Grid, change when the DataContext property XX change from an UserControl to another UserControl. Any idea how do that?

What I'm trying to achieve is a loosely couple UI consisting of a Shell (main window) with placeholders, but with the capability to allow the content of such placeholders be changed from any of the placeholders, How? well, binding some placeholder controls's Commands to the Shell DataContext's Commands (the ShellViewModel) that is by hierarchy the DataContext of all controls of any placeholder that don't overwrite the DataContext property, then inside the implementation of such commands change XX.

More specifically, I want that the navigation controls (UI) of my applications can be easily changed without affect the semantic of the navigation, and the only thing that come to my newbie mind after read about MVVM is the above approach. Do you have a better idea? Is that correct or is too loosely couple ;)

I Added this part after the first reply:

Data template don't fill the bill. Why? Well, simple, with data template the Shell must know at front all the possible part of the UI elements that will compose the application UI (the views that take UserControls form normally) and the idea here is avoid that. The idea is have a Shell with placeholders, and that the Shell expose Commands through its DataContext (the Shell's ViewModel) to any of the UNKNOWN AT FRONT UIs components that will fill such placeholders. Such commands can be saw as a kind of API/Interface used to interact with the navigation system.

After research more, I get an idea so one ANSWER can be some thing like the follow:

<ContentControl Grid.Row="0">
    <Binding Path="CurrentView"/>
 </ContentControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-10-26 02:22:04

我不确定我是否完全理解你的问题,但是 数据模板选择器可能就是您正在寻找的。在您的情况下,返回的模板将根据当前的 DataContext 进行更改,模板将包含相应的 UserControl

I'm not sure if I understand your question fully, but a data template selector might be what you're looking for. In your case the returned template would be changed based on the current DataContext, the template would contain the repspective UserControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文