Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 639
heltonbiker Avatar asked Aug 17 '12 20:08

heltonbiker


2 Answers

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.

like image 93
nemesv Avatar answered Nov 14 '22 00:11

nemesv


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);
like image 31
JazzSoft Avatar answered Nov 14 '22 00:11

JazzSoft