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:
foreach (string columnName in MyObject.Columns)
{
GridViewColumn column = new GridViewColumn();
column.DisplayMemberBinding = new Binding("MyObject." + columnName);
column.Header = columnName;
column.Width = 50;
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