I have found some articles to get data to be put into a listview with a databinding on gridviewcolums but I am needing to retrieve this data when a line is selected from the list, this is the code i have to fill the list and "attempt" to retrieve the data. As far as i have found, please have very different ways of putting this data into a listview and I assume my problem is that I do not know the correct way to retrieve it with it formatted in the XAML this way.
private void Button_Click_1(object sender, RoutedEventArgs e)
{ // This is When a button is clicked to populate the List
Operate op = new Operate();
List<object> users = op.GetUser();
if (users != null)
{
ResultsView.Items.Clear();
foreach (UserPrincipal user in users)
{
ResultsView.Items.Add(new {Col1 = user.GivenName, Col2 = user.Surname, Col3 = user.SamAccountName});
}
}
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ // This is When a line is selected and should get the info from the line
if (ResultsView.SelectedItem != null)
{
string sel = ResultsView.SelectedItem.Col1;
}
}
And this is the XAML for the list
<ListView
x:Name="ResultsView"
Margin="5,5,5,5"
SelectionMode="Single"
SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="H1" DisplayMemberBinding="{Binding Col1}"/>
<GridViewColumn Width="100" Header="H2" DisplayMemberBinding="{Binding Col2}"/>
<GridViewColumn Width="100" Header="H3" DisplayMemberBinding="{Binding Col3}"/>
</GridView>
</ListView.View>
</ListView>
Very sorry about any weird formatting or errors, just starting with this a few days ago. Here is a picture of the list when it is populated, looks as I expect it to.
My answer was in Travis J's comment somewhat. I knew that I may have needed to create this table in a different way so that the output of SelectedItem does not come up as anonymous type. So I have redone how the table is constructed and am now using DataTable.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Operate op = new Operate();
DataTable users = op.GetUser();
if (users != null)
{
ResultsView.ItemsSource = users.DefaultView;
}
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ResultsView.SelectedItem != null)
{
DataRowView line = ResultsView.SelectedItem as DataRowView;
string stuff = line.Row.ItemArray[2].ToString();
MessageBox.Show(stuff);
}
}
And this made a need for the XAML to change so that the binding is looking for a path from the datatable.
<ListView
x:Name="ResultsView"
Margin="5,5,5,5"
SelectionMode="Single"
SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="H1" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Width="100" Header="H2" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Width="100" Header="H3" DisplayMemberBinding="{Binding Path=UserName}"/>
</GridView>
</ListView.View>
</ListView>
I have decided not to include my own method for creating the datatable but I will provide the link that showed me the quickest way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.