Reputation: 171
I am trying to get data from a listview box and have been unsuccessful.
<ListView Name="lst_CallData" Width="950" Height="500" Grid.Row="1" Grid.ColumnSpan="2" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Col0}" Width="150" />
<GridViewColumn Header="Num" DisplayMemberBinding="{Binding Col1}" Width="200" />
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Col2}" Width="200" />
</GridView>
</ListView.View>
</ListView>
I am trying to get the data out of Col2 but all avenues I have tried have only given me a string of all the data in the list. I tried to use:
DataRow selectedRow = lst_CallData.SelectedItem as DataRow;
but datarow does not exists in the current context.
Upvotes: 0
Views: 1731
Reputation: 848
The following is the way I tried and able to get data row or entire table object:
[XAML]
<Grid>
<Grid.DataContext>
<localData:MyData />
</Grid.DataContext>
<ListView Name="lst_CallData" Width="500" ItemsSource="{Binding MyTable}" SelectionChanged="lst_CallData_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Col0}" Width="150" />
<GridViewColumn Header="Num" DisplayMemberBinding="{Binding Col1}" Width="200" />
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Col2}" Width="200" />
</GridView>
</ListView.View>
</ListView>
</Grid>
[C#]
private void lst_CallData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataRowView selectedRowView = this.lst_CallData.SelectedItem as DataRowView;
if(selectedRowView != null)
{
DataRow selectedRow = selectedRowView.Row; // To get the partcular data row
DataTable inputTable = selectedRowView.DataView.Table; //To get the entire table data
}
}
Upvotes: 0
Reputation: 69959
When writing WPF, you have to take the time to organise your data type classes properly. By this, I mean don't use a horrible old DataTable
with its antiquated DataRow
elements... instead, define your own class, One that suits the purpose exactly. Importantly, if you have your own class, then you can start to take advantage of interfaces like INotifyPropertyChanged
and INotifyDataErrorInfo
to help with automatic UI updates and data validation.
If you created a class for your data, then you could have an ObservableCollection
full of them in a property, lets say named SomeCollection
. If you added another property of the type of your class named SomeItem
, you could then data bind these properties to the ListView
as @HighCore correctly showed you:
<ListView ItemSource="{Binding SomeCollection}" SelectedItem="{Binding SomeItem}" />
Then to access the selected item at any time from the code behind or view model, you can just access the SomeItem
property. As this property is of the type of your custom class, then you can now simply access any of its properties by name. For example, if you had the relevant properties and the need, you could do something like this:
public void SaveTextFile()
{
File.WriteAllText(SomeItem.FilePath, SomeItem.Text);
}
So anyway, that is what HighCore was eluding to and the way that MVVM developers would suggest, but there is a much quicker way too. If you have data bound items in the ListView
, then you can just access the ListView.SelectedItem
property directly from the code behind if you have named your ListView
:
YourDataType selectedItem = (YourDataType)lst_CallData.SelectedItem;
... where YourDataType
is the type of objects in the collection that is data bound to the ListView
Upvotes: 1