动态绑定GridView CellTemplate内容(Dynamically bind GridView CellTemplate content)

编程入门 行业动态 更新时间:2024-10-28 15:29:11
动态绑定GridView CellTemplate内容(Dynamically bind GridView CellTemplate content)

我正在使用带有ListView控件的GridView来显示一些目录内容。 我正在使用代码隐藏动态加载ListView内容,创建GridViewColumns并将它们绑定到我的自定义catListItem类中的属性。

var view = new GridView(); var binding = new Binding("Name"); var resElement = _mResourceManager.GetElementByMdlID("vlu_usw_name_of"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding }); binding = new Binding("Number"); resElement = _mResourceManager.GetElementByMdlID("vlu_usw_arc_logical_nmbr"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding });

一切都很好,但现在我正在尝试使用GridViewColumn的CellTemplate属性添加带有一些图标的列。 像这样的东西:

var view = new GridView(); var col = new GridViewColumn { Header = "" }; var template = new System.Windows.DataTemplate(typeof(Image)); col.CellTemplate = template; view.Columns.Add(col); var binding = new Binding("Name"); var resElement = _mResourceManager.GetElementByMdlID("vlu_usw_name_of"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding }); binding = new Binding("Number"); resElement = _mResourceManager.GetElementByMdlID("vlu_usw_arc_logical_nmbr"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding });

我知道使用DisplayMemberBinding,CellTemplate和CellTemplateSelector时有一个优先级。 所以我的问题是:我如何创建(设置,...)CellTemplate的内容,并可能动态地将其绑定到我的自定义类的属性? 我不知道我错过了什么! 我搜索过这个问题,但我发现的一切都是使用DataTemplate的XAML解决方案。 用后面的代码来做这件事很重要。 提前致谢!

I'm using GridView with a ListView control to show some catalog content. I'm loading the ListView content dynamically with code behind, creating GridViewColumns and binding them to properties in my custom catListItem class.

var view = new GridView(); var binding = new Binding("Name"); var resElement = _mResourceManager.GetElementByMdlID("vlu_usw_name_of"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding }); binding = new Binding("Number"); resElement = _mResourceManager.GetElementByMdlID("vlu_usw_arc_logical_nmbr"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding });

Everything is fine, but now I'm trying to add column with some icon, using the CellTemplate property of the GridViewColumn. Something like this:

var view = new GridView(); var col = new GridViewColumn { Header = "" }; var template = new System.Windows.DataTemplate(typeof(Image)); col.CellTemplate = template; view.Columns.Add(col); var binding = new Binding("Name"); var resElement = _mResourceManager.GetElementByMdlID("vlu_usw_name_of"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding }); binding = new Binding("Number"); resElement = _mResourceManager.GetElementByMdlID("vlu_usw_arc_logical_nmbr"); view.Columns.Add(new GridViewColumn { Header = resElement.Name, DisplayMemberBinding = binding });

I know there's a priority when using DisplayMemberBinding, CellTemplate and CellTemplateSelector. So my question is: How can I create (set, ...) content of the CellTemplate and probably bind it to a property of my custom class dynamically? I don't know what do I miss! I've searched for that issue, but everything I found is XAML solutions using DataTemplate. It's important to do it with code behind. Thanks in advance!

最满意答案

其实我在这里找到了解决方案

我不得不使用FrameworkElementFactory。 这是我要找的代码:

var column = new GridViewColumn { Header = "" }; var customTemplate = new System.Windows.DataTemplate(); var efImage = new FrameworkElementFactory(typeof(Image)); efImage.SetBinding(Image.SourceProperty, new Binding("Icon")); customTemplate.VisualTree = efImage; column.CellTemplate = customTemplate; view.Columns.Add(column);

Actually I found the solution HERE

I had to use FrameworkElementFactory. Here's the code I was looking for:

var column = new GridViewColumn { Header = "" }; var customTemplate = new System.Windows.DataTemplate(); var efImage = new FrameworkElementFactory(typeof(Image)); efImage.SetBinding(Image.SourceProperty, new Binding("Icon")); customTemplate.VisualTree = efImage; column.CellTemplate = customTemplate; view.Columns.Add(column);

更多推荐

CellTemplate,binding,使用,using,var,电脑培训,计算机培训,IT培训"/> <meta name=&

本文发布于:2023-04-29 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   内容   动态   GridView   CellTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!