我正在使用带有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=&
发布评论