0

I created a listview. I need to get the user's input from the Textbox inside the listview. When user click on submit button User input from listview are displayed in a MessageBox. How to get user input from textbox inside the listview

<ListView x:Name="lstvQualification"
            Height="96"
            Margin="10,6,14,0"
            VerticalAlignment="Top">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="249" Header="Education">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox x:Name="txtEducation"
                                        Width="247"
                                        Text="{Binding education}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="253" Header="College/Institution">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox x:Name="txtCollege"
                                        Width="251"
                                        Text="{Binding college}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="88" Header="Mark(%)">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox x:Name="txtMark"
                                        Width="86"
                                        Text="{Binding mark}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="88" Header="Add">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button x:Name="btnAddQualification"
                                    Click="btnAddQualification_Click"
                                    Content="Add"
                                    Style="{StaticResource NewImg}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

<Button Content="Submit" x:name="submit" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sabir
  • 235
  • 4
  • 15
  • You have a lot of unnecessary StackPanels. If you only have a single item in your DataTemplate you don't need to wrap a container around it. You can safely delete all of them. As a general style pointer, 'education', 'college' and 'mark' are all properties on the object being passed into the ListView. These should be Capitalised. – goobering Jul 30 '15 at 08:50

2 Answers2

1

You already use {Binding ...} everywhere in the cell templates, those bindings are TwoWay by default, so any user input would/should automatically be passed to underlying data objects. However, I don't see any binding on ListView.ItemsSource property, so most probably you forgot to prepare the dataobjects. Create a collection of dataitems, bind them to the ItemsSource, let the ListView display them and celltemplates with bindings should update the dataitems' properties with no extra work from your side. When submit is pressed, just check the collection of dataitems, all the data entered should be already there (assuming you made all models and bindings properly).

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • Gives one example plz – Sabir Jul 30 '15 at 08:32
  • @ShahidNeermunda: [Sample code on MSDN](https://msdn.microsoft.com/en-us/library/ms771277(v=vs.85).aspx) - see the "download sample" link. [Another info here on stack](http://stackoverflow.com/questions/5652527/editable-wpf-listview). [and yet another idea](http://blogs.msdn.com/b/atc_avalon_team/archive/2006/03/14/550934.aspx).. please just search for it, there are many other samples and ideas. It all depends on how you would like it to be done and behave. – quetzalcoatl Jul 30 '15 at 08:54
1

This could be the example for change in your code to work fine:

<ListView x:Name="myListView" 
                  HorizontalAlignment="Stretch" 
                  ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}">

            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='Contact'}" Header="Contact"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='Qty'}" Header="Q"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='Amount'}" Header="Amt"/>
                    <GridViewColumn x:Name="tbTot" Header="Tot">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBox LostFocus="TextBox_LostFocus" Width="100" Text="{Binding XPath='Text'}" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>    
        </ListView>
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43