How to Add Row of a WPF Listview when Press “Enter” Key in Listview Column


I saw many application software provides this kind of facilities.But WPF is new for everybody.So i think for doing this in c# WPF.In this post i will discuss about this solution.
My ListView contain one column which is binding by a object.At first i make a Listview which Column field contain textbox.Here is the XAML Code.

<ListView Height="101" Margin="289,318,0,0" Name="lvItem" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Width="215">
    <ListView.View>	
        <GridView>
            <GridViewColumn Header="Item Name"  Width="80">
                <GridViewColumn.CellTemplate >
		    <DataTemplate>
                        <TextBox Name="txtForAddRow"  Text="{Binding Path=ItemName}" 
                                 Margin="-6,0,-6,0" KeyDown="txtForAddRow_KeyDown" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>  

The entity class definition is here

public class EItemCategory
{
public string ItemName { get; set; }
}

Now the cs Code for add row in Listview

private void txtForAddRow_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key==Key.Enter)
{
EItemCategory obj = new EItemCategory();
lvItem.Items.Add(obj);
}
}

If you have any query about this topic please leave a comment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: