WPF - set DataTemplate for programmatically added GridViewColumns

Could not find an answer to this question.

I have a ListView WPF control that can contain a different number of columns. For example, it can display customer data, display columns Id, Name, Email, etc., Or it can contain products that display ID, Name, Price, NumberInStock, Manufacturer, and you get the idea: a different number of columns, distinguished names.

I want certain columns to display data in different ways. For example, instead of printing “Yes” or “No” as the value of the NumberInStock column, I want to display a neat image.

If I had a fixed number of columns, with fixed names for binding, I would see how easy it is. Just define a DataTemplate for this particular column, and I would use this to determine the presentation of my column. However, I cannot figure out how to do this in my situation.

I am very new to WPF, so excuse me if my approach is bad :-) In my XAML, I defined a ListView control that is pretty empty. In my code behind, I use:

    // get all columns from my objects (which can be either a Customer of Product)
    foreach (string columnName in MyObject.Columns)
        {
          GridViewColumn column = new GridViewColumn();
          // Bind to a property of my object
          column.DisplayMemberBinding = new Binding("MyObject." + columnName);
          column.Header = columnName;
          column.Width = 50;
          // If the columnname is number of stock, set the template to a specific datatemplate defined in XAML
          if (columnName == "NumberInStock")
            column.CellTemplate = (DataTemplate)FindResource("numberInStockImageTemplate");
          explorerGrid.Columns.Add(column);
        }

Ok, I'm sure this can be done a little prettier (if you have any tips, please!), But the biggest problem is that I don't see the differences in the column. It simply displays the text value of the NumberInStock column. My DataTemplate is defined in XAML:

<Window.Resources>
<DataTemplate x:Name="NumberInStock" x:Key="NumberInStock">
      <Border BorderBrush="Red" BorderThickness="2.0">
        <DockPanel>
          <Image Width="24" Height="24" Margin="3,0" Source="..\Images\instock.png" />
        </DockPanel>
      </Border>
</DataTemplate>
</Window.Resources>

, , "" "" NumberInStock, 2. ListView!

, Razzie

+5
3

.

DisplayMemberBinding CellTemplate . DisplayMemberBinding CellTemplate.

MSDN:

: , :

* DisplayMemberBinding
* CellTemplate
* CellTemplateSelector

. # Disciples post .

+7

        GridViewColumn column = new GridViewColumn { Header = "IM" };            
        DataTemplate template = new DataTemplate();

        FrameworkElementFactory factory  =  new FrameworkElementFactory(typeof(Grid));
        template.VisualTree = factory;
        FrameworkElementFactory imgFactory  =  new FrameworkElementFactory(typeof(Image));

        Binding newBinding  =  new Binding("IMG");
        imgFactory.SetBinding(Image.SourceProperty,newBinding);
        imgFactory.SetValue(Image.WidthProperty,15.0);
        imgFactory.SetValue(Image.HeightProperty, 15.0);

        factory.AppendChild(imgFactory);
        column.CellTemplate = template;
        view.Columns.Add(column);

        ListViewMain.View = view;

+2

I think the problem is that the string you pass to FindResource()does not match the resource key that you defined in XAML. Try passing "NumberInStock"and see if this works.

0
source

All Articles