Adding data (not only text) to a multi column ListView (WPF)
- by user811804
I am working on a WPF application in C# (.NET 4.0) where I have a ListView with a GridView that has two columns.
I dynamically want to add rows (in code). My dilemma is that only the first column will have regular text added to it. The second column will have an object that includes a multi column Grid with TextBlocks. (see link http://imageshack.us/photo/my-images/803/listview.png/)
If I do what you would normally do when you want to enter text in all columns (ie. DisplayMemberBinding) all I get in the second column is the text "System.Windows.Grid", which obviously isn't what I want.
For reference if I just try to add the Grid object (with the TextBlocks) with the code listView1.Items.Add(grid1) (not using DisplayMemberBinding) the object gets added to the second column only (with the first column being blank) and not how it normally works with text where the same text ends up in all columns.
I hope my question is detailed enough and any help with this would be much appreciated.
EDIT:
I have tried the following code, howeever every time I click the button to add a new row every single row gets updated with the same datatemplate. (ie. the second column always shows the same data on every row.)
xaml:
<Window x:Class="TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="AAA" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="grid1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="374*" />
<ColumnDefinition Width="129*" />
</Grid.ColumnDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="21,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button1_Click" />
</Grid>
code:
public partial class MainWindow : Window
{
ListView listView1 = new ListView();
GridViewColumn viewCol2 = new GridViewColumn();
public MainWindow()
{
InitializeComponent();
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty,
HorizontalAlignment.Stretch));
listView1.ItemContainerStyle = style;
GridView gridView1 = new GridView();
listView1.View = gridView1;
GridViewColumn viewCol1 = new GridViewColumn();
viewCol1.Header = "Option";
gridView1.Columns.Add(viewCol1);
viewCol2.Header = "Value";
gridView1.Columns.Add(viewCol2);
grid1.Children.Add(listView1);
viewCol1.DisplayMemberBinding = new Binding("Option");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));
Random random = new Random();
int cols = random.Next(1, 6);
int full = 100;
for (int i = 0; i < cols; i++)
{
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
int partWidth = random.Next(0, full);
full -= partWidth;
col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(partWidth, GridUnitType.Star));
spFactory.AppendChild(col1);
}
if (full > 0)
{
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(full, GridUnitType.Star));
spFactory.AppendChild(col1);
}
for (int i = 0; i < cols; i++)
{
FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
SolidColorBrush sb1 = new SolidColorBrush();
switch (i)
{
case 0:
sb1.Color = Colors.Blue;
break;
case 1:
sb1.Color = Colors.Red;
break;
case 2:
sb1.Color = Colors.Yellow;
break;
case 3:
sb1.Color = Colors.Green;
break;
case 4:
sb1.Color = Colors.Purple;
break;
case 5:
sb1.Color = Colors.Pink;
break;
case 6:
sb1.Color = Colors.Brown;
break;
}
text1.SetValue(TextBlock.BackgroundProperty, sb1);
text1.SetValue(Grid.ColumnProperty, i);
spFactory.AppendChild(text1);
}
if (full > 0)
{
FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
SolidColorBrush sb1 = new SolidColorBrush(Colors.Black);
text1.SetValue(TextBlock.BackgroundProperty, sb1);
text1.SetValue(Grid.ColumnProperty, cols);
spFactory.AppendChild(text1);
}
dataTemplate.VisualTree = spFactory;
viewCol2.CellTemplate = dataTemplate;
int rows = listView1.Items.Count + 1;
listView1.Items.Add(new { Option = "Row " + rows });
}
}