0

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!

Tihomir Blagoev
  • 83
  • 1
  • 1
  • 10
  • why you are using DataTemplate of Image, it will return default template for Image, you must be having your custom DataTemplate with name, use that instead of typeof(Image) – Mitan Shah Jun 03 '15 at 12:37
  • @MitanShan Thank you for your answer, but I'm not sure I understand your point. Is it compulsory to create custom DataTemplate? I cannot find a way to do it with code behind. Isn't it possible to bind the default template of image to my custom property? – Tihomir Blagoev Jun 03 '15 at 13:01
  • it will not help because Image template will require properties related to Image Control, you have to define DataTemplate in code if you want. – Mitan Shah Jun 03 '15 at 13:07

1 Answers1

0

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);
Community
  • 1
  • 1
Tihomir Blagoev
  • 83
  • 1
  • 1
  • 10