How to add ListViewItem to WPF multicolumn ListView by hand (no binding) in XAML
Asked Answered
A

2

8

I have a problem with ListView design in Expression Blend that is harder than I thought it should.

I'd like to just draw a screen using XAML. This WILL NOT run inside an application, is just a static design study that should be rendered for viewing in the design window of Expression Blend, exclusively.

I have this so far:

<ListView x:Name="examList" SelectionMode="Single">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date" Width="Auto"/>
            <GridViewColumn Header="Setup" Width="Auto"/>
            <GridViewColumn Header="Protocol" Width="Auto" />
            <GridViewColumn Header="Channels" Width="Auto"/>
            <GridViewColumn Header="Duration" Width="Auto"/>
        </GridView>
    </ListView.View>

    <ListViewItem>
        <TextBlock Text="stuff" />  <!-- what should I put here??? -->
    </ListViewItem>
</ListView>

The problem is: I don't have a clue how I should create ListViewItems with one string or number value for each field (each column in the GridView).

I would appreciate any help, with or without code-behind, but a necessary requirement for my workflow is that it renders at design time inside Expression Blend without the need to run the application, and preferrably that the data is not bound from another file, but entered by hand directly into the XAML (I don't mind it, actually I WANT it).

I found this answer, but it doesn't do what I need, I think.

Thanks for reading!

Akira answered 17/8, 2012 at 20:25 Comment(1)
Something like this perhaps?Gallous
P
9

You can use a string array inside your ListViewItem and use the DisplayMemberBinding to specify which which index should be displayed in which column.

<ListView x:Name="examList" SelectionMode="Single">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date" Width="Auto" 
                            DisplayMemberBinding="{Binding [0]}"/>
            <GridViewColumn Header="Setup" Width="Auto" 
                            DisplayMemberBinding="{Binding [1]}"/>
            <GridViewColumn Header="Protocol" Width="Auto" 
                            DisplayMemberBinding="{Binding [2]}"/>
            <GridViewColumn Header="Channels" Width="Auto" 
                            DisplayMemberBinding="{Binding [3]}" />
            <GridViewColumn Header="Duration" Width="Auto" 
                            DisplayMemberBinding="{Binding [4]}"/>
        </GridView>
    </ListView.View>

    <ListViewItem>
        <x:Array Type="{x:Type sys:String}">
                <sys:String>This is Date</sys:String>
                <sys:String>This is Setup</sys:String>
                <sys:String>This is Protocol</sys:String>
                <sys:String>This is Channels</sys:String>
                <sys:String>This is Duration</sys:String>
            </x:Array>        
    </ListViewItem>
</ListView>

Where sys: is xmlns:sys="clr-namespace:System;assembly=mscorlib"

Or you can create your own datatype which will hold the values:

public class Exam
{
    public string Date { get; set; }

    public string Setup { get; set; }

    //...
}

And use it in the ListViewItem:

<ListView x:Name="examList" SelectionMode="Single">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date" Width="Auto" 
                            DisplayMemberBinding="{Binding Date}"/>
            <GridViewColumn Header="Setup" Width="Auto" 
                            DisplayMemberBinding="{Binding Setup}"/>    
        </GridView>
    </ListView.View>

    <ListViewItem>
        <local:Exam Date="2001/1/1" Setup="Some setup" />
    </ListViewItem>
</ListView>

Where local: point to your Exam class namespace.

Pika answered 17/8, 2012 at 21:7 Comment(2)
Sounds like what I was looking for. Cannot test it now, will come back later to (most probably) accept. Thank you!Akira
I used the second option, since it is cleaner/less-verbose. Worked very fine!Akira
C
5

You need index bindings like "[0]".

<Grid>
    <ListView x:Name="lv" />
</Grid>



lv.Items.Clear();
var gv = new GridView();
lv.View = gv;

var columns = new List<string> { "Date", "Setup", "Protocol", "Channels", "Duration" };
for(int index = 0; index < columns.Count; index++)
{
    gv.Columns.Add(new GridViewColumn
    {
        Header = columns[index],
        DisplayMemberBinding = new Binding("[" + index.ToString() + "]")
    });
}

// Populate list
var row1 = new List<string> { "Date1", "Setup1", "Protocol1", "Channels1", "Duration1" };
var row2 = new List<string> { "Date2", "Setup2", "Protocol2", "Channels2", "Duration2" };
lv.Items.Add(row1);
lv.Items.Add(row2);
Clone answered 27/3, 2017 at 2:50 Comment(1)
this is an unappreciated answer! it is exactly the missing link I was looking for DisplayMemberBinding = new Binding("[" + index.ToString() + "]")Fustigate

© 2022 - 2024 — McMap. All rights reserved.