.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > How add just an item each time into listView using c#?
 

How add just an item each time into listView using c#?

Hi,

I'm with problem in adding an anitem into listview... Im my application has a class who calls "Items".

Well, this class has some attributes:

first item: textBox;
second item: textbox;
third item: stackPanel where there are two items: Button and textBlock;
forth item: checkBox;

I create a object taht has a kind of "Items". I want to add this object into listview.


How to do it?

Do I have to create a gridView object then I insert an Items object into gridview object?

Does anybody has a tutorial or some example?

Can you help me? please?

Thank you. I hope anybody help me. bye
Carlos Marchi

Hi,

Can nobody help me? please

Carlos Marchi

Hi Carlos,

you simply have to call the Add-Method on the Items-Property of the ListView. Pass in your object and that's all. If your object is of type UIElement, it will be rendered, if it's not of type UIElement, the ToString-Method is called and the result is displayed inside of a TextBlock.

To not get the ToString-Result, you could create either a DataTemplate for your Non-UIElement or you could set the View-Property of the ListView to GridView and bind the Columns to the Properties of your object.

For adding Items in C#, you can look at this example, even if it's used to show routed events:

http://www.thomasclaudiushuber.com/blog/2007/12/12/c-30-and-wpfs-listview/

Thomas Claudius Huber

Hi Thomas

Thank you for helping me...

I was reading the site taht you gave me, then I hadany doubts...

for example:

I created a class: its name is: Items

public class Items

{

private TextBox item;

private TextBox name;

private StackPanel image;

private CheckBox result;

public Items()

{

item = new TextBox();

name = new TextBox();

createImage();

result = new CheckBox();

}

private createImage()

{

image = new StackPanel();

Button b = new Button();

b.Content = "Insert";

image.Children.Add(b);

TextBlock t = new TextBlock();

t.Text = " ...";

image.Children.Add(t);

}

public TextBox Item

{

get{return this.item;}

}

public TextBox Name

{

get{return this.name;}

}

publicStackPanel Image

{

get{return this.image}

}

publicCheckBox Result

{

get{return this.result;}

}

}

in application has a listView...

I create a new Items object, then I add into listView...

How to do it in code behind?

Thomas, see this code under:

<ListView x:Name="listView" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="RoutedEvent" DisplayMemberBinding="{Binding RoutedEventName}"/>
<GridViewColumn Header="Sender"
DisplayMemberBinding="{Binding Sender}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</
Window>

Thereis a DisplayMemberBinding="{Binding RoutedEventName}"... Howput the Items class here?

wouldit right be it?:

<ListView x:Name="listView" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Item" DisplayMemberBinding="{Binding Item}"/>
<GridViewColumn Header="Name"DisplayMemberBinding="{Binding Name}"/>

<GridViewColumn Header="Image" DisplayMemberBinding="{Binding Image}"/>
<GridViewColumn Header="Result"DisplayMemberBinding="{Binding Result}"/>

</GridView>
</ListView.View>
</ListView>
</Grid>
</
Window>

Is it right this code?

What do you think?

Thank you

Carlos Marchi

Could you tell me that what is the result you hope? Because, base on my understanding, you want to bind the controls to a ListView. But, the controls is used for defining user interfaces, and we bind the data source (such as DataSet and collections, etc) to the controls. I think that the following links may help for you.

http://msdn2.microsoft.com/en-us/library/ms771480.aspx

http://msdn2.microsoft.com/en-us/library/ms771780.aspx

http://msdn2.microsoft.com/en-us/library/ms754239.aspx

http://msdn2.microsoft.com/en-us/library/ms750612.aspx

Best Regards,

Wei Zhou

Wei Zhou - MSFT

Here's a sample that may help you. Needless to say you will need to take care of appropriate styling. Also, I have used XmlDataProvider. However you can bind to a .net object as well.

Code Snippet

<Window x:Class="TestWPFApp.XMLTestWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

Title="XMLTestWindow" Height="300" Width="300">

<Window.Resources>

<XmlDataProvider x:Key="xmlFileRes" XPath="persons/person">

<x:XData>

<persons xmlns="">

<person name="Atul Gupta" age="25" SSN="1234" married="true"/>

<person name="Carlos" age="25" SSN="1234" married="false"/>

<person name="Wei z" age="25" SSN="1234" married="true"/>

</persons>

</x:XData>

</XmlDataProvider>

</Window.Resources>

<StackPanel>

<ListView VerticalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource xmlFileRes}}">

<ListView.View>

<GridView>

<GridViewColumn Header="Name">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@name}" Width="70" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Age">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@age}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="SSN">

<GridViewColumn.CellTemplate>

<DataTemplate>

<StackPanel Orientation="Horizontal">

<TextBlock Text="{Binding XPath=@SSN}" Width="40" />

<Button Content="Click" />

</StackPanel>

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Maried">

<GridViewColumn.CellTemplate>

<DataTemplate>

<CheckBox IsChecked="{Binding XPath=@married}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>

</StackPanel>

</Window>

Atul Gupta

Hi,

Thank you for trying to solve my problem. I'm finding various sites, and three book and I dont find a solution.

Well, I'm going to explain better it...

In the application has a class. It calls "Items"

this is its code:

public class Items

{

private TextBox item;

private TextBox name;

private StackPanel image;

private CheckBox result;

public Items()

{

item = new TextBox();

name = new TextBox();

createImage();

result = new CheckBox();

}

private createImage()

{

image = new StackPanel();

Button b = new Button();

b.Content = "Insert";

image.Children.Add(b);

TextBlock t = new TextBlock();

t.Text = " ...";

image.Children.Add(t);

}

public TextBox Item

{

get{return this.item;}

}

public TextBox Name

{

get{return this.name;}

}

publicStackPanel Image

{

get{return this.image}

}

publicCheckBox Result

{

get{return this.result;}

}

there is insede the window a listview control:

I want this form for it:

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Item1 | Item2 | Item3 |Item4 |

-------------------------------------------------------------------------------------------------------------------------------------------------------------

TextBox| TextBox | Button and TextBlock | CheckBox |

--------------------------

| ADDButton |

--------------------------

How can see, each column has a type specify control.

First Column has a TextBox

Second Column has a other textBox

Third Column has two controls: a button and textblock who are insede a stackPanel

fourth Column has a checkBox

What i need to do is: I create a Items object then I insert this object into listview.

The problem htat i dont know how to do it...I still need to know what line and column I clicked...

How Can I know what line and column I selected?

For example:

There are 10 lines into listview, I clicked on third line andthird column.. How can I knowwhat line and colun I clicked?

I was thinkingabout using dataTable, however I dont know how todo to insertItems object into the datatable.

DoesAnybody have an idea??

Thank you

Carlos Marchi

Why does your class has members as UI controls?

Are you trying to create an XLS type interface where there are many editable rows and user will type data into rows?

Atul Gupta

Hi Atul,

Thank you for helping me.

I was seeing this code and I would like to know to create this under code in code behind in c#:

<XmlDataProvider x:Key="xmlFileRes" XPath="persons/person">

<x:XData>

<persons xmlns="">

<person name="Atul Gupta" age="25" SSN="1234" married="true"/>

<person name="Carlos" age="25" SSN="1234" married="false"/>

<person name="Wei z" age="25" SSN="1234" married="true"/>

</persons>

</x:XData>

</XmlDataProvider>

Do you know how do it? How do add a just item into listview using xmlDataProvider?

Thank youu

Carlos Marchi

Base on my understanding, you should not use a class with controls to do this, because we are unable to add new row to the ListView control. You should use data binding with DataTemplate to do this. Here is the example.

Code Snippet

<Window

x:Class="ForumProjects.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Width="800" Height="600">

<Window.Resources>

<XmlDataProvider x:Key="xmlFileRes" XPath="persons/person">

<x:XData>

<persons xmlns="">

<person name="Atul Gupta" age="25" SSN="1234" married="true"/>

<person name="Carlos" age="25" SSN="1234" married="false"/>

<person name="Wei z" age="25" SSN="1234" married="true"/>

</persons>

</x:XData>

</XmlDataProvider>

<DataTemplate x:Key="nameTemplate">

<TextBox Text="{Binding XPath=@name}"/>

</DataTemplate>

<DataTemplate x:Key="ageTemplate">

<TextBox Text="{Binding XPath=@age}"/>

</DataTemplate>

<DataTemplate x:Key="ssnTemplate">

<StackPanel Orientation="Horizontal">

<Button>S</Button>

<TextBlock Text="{Binding XPath=@SSN}"/>

</StackPanel>

</DataTemplate>

<DataTemplate x:Key="marriedTemplate">

<CheckBox IsChecked="{Binding XPath=@married}"/>

</DataTemplate>

</Window.Resources>

<ListView ItemsSource="{Binding Source={StaticResource xmlFileRes}}">

<ListView.View>

<GridView>

<GridViewColumn Header="Name" CellTemplate="{StaticResource nameTemplate}"/>

<GridViewColumn Header="Age" CellTemplate="{StaticResource ageTemplate}"/>

<GridViewColumn Header="SSN" CellTemplate="{StaticResource ssnTemplate}"/>

<GridViewColumn Header="Married" CellTemplate="{StaticResource marriedTemplate}"/>

</GridView>

</ListView.View>

</ListView>

</Window>

Best Regards,

Wei Zhou

Wei Zhou - MSFT

Wei, in your example, you have pulled out the DataTemplate from the GridViewColumn and put them as separate resources. Is there a reason to prefer this approach over directly embedding the DataTemplates inside of GridViewColumn?

In the code I wrote, I at least don't have to keep scolling back and forth to check on what each of the StaticResources mean?

Atul Gupta

This is only my personal habit, I feel it can let me focus on main UI logic and write more less code. Smile

Best Regards,

Wei Zhou

Wei Zhou - MSFT

Hi,

Thank you very much!

Well,

I didnt understant anything.

In window I did it:

<ListView x:Name="listView" VerticalAlignment="Stretch" >

<ListView.View>

<GridView>

<GridViewColumn Header="Item" Width="50">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@item}" Width="70" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Alternativa" Width="420">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@descrisao}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Imagem" Width="200">

<GridViewColumn.CellTemplate>

<DataTemplate>

<StackPanel Orientation="Horizontal">

<Button Content="Inserir" />

<TextBlock Text="{Binding XPath=@caminhoImagem}" Width="40" />

</StackPanel>

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Correta(s)" Width="75">

<GridViewColumn.CellTemplate>

<DataTemplate>

<CheckBox IsChecked="{Binding XPath=@correta}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>

Fianlly, The window is it:

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Item1 | Item2 | Item3 |Item4 |

-------------------------------------------------------------------------------------------------------------------------------------------------------------

TextBox| TextBox | Button and TextBlock | CheckBox |

--------------------------

| ADDButton |

--------------------------

is it right what I did?

Some person is going to click in add button then it will put a new line into listview. As you see, each line has four item: textbox, textbox, (button and textblock) and checkbox.

I create a class:

public class Items

{

privatestring item;

privatestring name;

privatestring image;

privatebool result;

public Items()

{

item = "";

name = "";

image = "";

result = true;

}

publicstring Item

{

get{return this.item;}

set{item = value;}

}

publicstring Name

{

get{return this.name;}

seti{name = value;}

}

publicstring Image

{

get{return this.image}

set{image = value;}

}

publicbool Result

{

get{return this.result;}

set{result = value;}

}

this class will used to create a object line.

Now I need to create method that add a default line into listview, then the some person will be able fill out all the line.

well, I dont know how to do it.

Please, Does Anybody have an idea? I'm finding a various places and I didnt find anything to solve this problem.

thank you.

Carlos Marchi

Hi,

Thank you very much!

Well,

I didnt understant anything.

InXAML I did it:

<ListView x:Name="listView" VerticalAlignment="Stretch" >

<ListView.View>

<GridView>

<GridViewColumn Header="Item" Width="50">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@item}" Width="70" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Alternativa" Width="420">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBox Text="{Binding XPath=@descrisao}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Imagem" Width="200">

<GridViewColumn.CellTemplate>

<DataTemplate>

<StackPanel Orientation="Horizontal">

<Button Content="Inserir" />

<TextBlock Text="{Binding XPath=@caminhoImagem}" Width="40" />

</StackPanel>

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn Header="Correta(s)" Width="75">

<GridViewColumn.CellTemplate>

<DataTemplate>

<CheckBox IsChecked="{Binding XPath=@correta}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>

Fianlly, The window is it:

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Item1 | Item2 | Item3 |Item4 |

-------------------------------------------------------------------------------------------------------------------------------------------------------------

TextBox| TextBox | Button and TextBlock | CheckBox |

--------------------------

| ADDButton |

--------------------------

is it right what I did?

Some person is going to click in add button then it will put a new line into listview. As you see, each line has four item: textbox, textbox, (button and textblock) and checkbox.

I create a class:

public class Alternativa{

privatestring item;

privatestring name;

privatestring image;

privatebool result;

public Items()

{

item = "";

name = "";

image = "";

result = true;

}

publicstring Item

{

get{return this.item;}

set{item = value;}

}

publicstring Name

{

get{return this.name;}

seti{name = value;}

}

publicstring Image

{

get{return this.image}

set{image = value;}

}

publicbool Result

{

get{return this.result;}

set{result = value;}

}

this class will used to create a object line.

public class Alternativas: ObservableCollection<Alternativa>

{

#region Construtores

public Alternativas()

: base()

{

}

#endregion

}

Now I need to create method that add a default line into listview, then the some person will be able fill out all the line.

well, I dont know how to do it.

Please, Does Anybody have an idea? I'm finding a various places and I didnt find anything to solve this problem.

thank you.

Carlos Marchi

You can use google to search for other answers

Custom Search

More Threads

• Binding non content properties
• How can I make a mouseOver also cause a focus event?
• Image & scrollViewer
• DataTemplate issue ...
• WPF application hang. Hosting Windows forms control in WPF
• Dynamic UI with WPF and LINQ (generate XElements Dynamically)
• DataGrid / Lookup
• Data binding question (ObjectDataProvider attributes)
• Animation Value Binding
• How to bring to front one of the usercontrol objects?