Sql、Wpf、Xaml、C#、绑定数据、动态资源、访问非静态数据、获取对象的引用
好吧,我对 WPF 和 XAML 相当陌生,尽管我进行了搜索,但我找不到一个简单的解决方案,而且在我看来,我不会很快找到答案。
问题很简单,我创建了一个 WPF 项目,并在 SelectList.xaml 中有一个数据网格。一旦选择了一行,我将所选行保存在一个名为“类别”的对象中。到目前为止,一切正常,但我不知道如何从其他地方 temp.xaml 获取对该对象的引用?
非常感谢 任何帮助将不胜感激 干杯
Ok, well I am pretty pretty pretty new to WPF and XAML, despite my search I could not find a simple solution and it seems to me that I won't be able to find an answer pretty soon.
The question is so simple, I have created a WPF project and have a datagrid in SelectList.xaml Once a row selected, I save the selected row in an object say this object called "category". So far everything is ok but I can't figure out how I am going to obtain a reference to this object from an other place temp.xaml ?
Thanks very much
Any help will be highly appreciated
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 WPF 中提供间接通信的常见方法是利用调解器模式。您可以使用调解器来发布类别选择,并让临时视图订阅类别选择更改的通知。
请参阅 http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx 有关具体调解器的简单示例。如果您想要更健壮的实现,还有几种流行的 MVVM 框架可以提供中介器模式实现。
简单调解器实现:
SelectList.xaml 代码隐藏:
Temp.xaml 代码隐藏:
A common way to provide indirect communication in WPF is to leverage the Mediator pattern. You can use a mediator to publish the selection of your category, and have the temp view subscribe to notification of a change in selection of your category.
See http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx for a simple example of a concrete mediator. There are also several popular MVVM frameworks available that provide Mediator pattern implementations if you want a more robust implementation.
Simple Mediator implementation:
SelectList.xaml code-behind:
Temp.xaml code-behind:
创建一个可访问的方法(如果需要,可以公开),该方法接受“Temp.xaml”代码隐藏文件中此“类别”对象的引用。然后通过此方法将“category”对象从“SelectList.xaml”代码隐藏文件传递到“Temp.xaml”文件。
Create a accessibly method (public if you want to) which accepts the reference of this "category" object in the "Temp.xaml" code behind file. Then pass the "category" object from the "SelectList.xaml" code behind file to the "Temp.xaml" file through this method.