MCSharp
MCSharp

Reputation: 1068

How To Get ListView Cell Text Value From Binding Collection?

How can I get the text from a ListView selected cell?

I have a ListView that creates a list with a Button click. I'd like to retrieve the text value of the Car column with a right click. I don't know what the correct command for this is. SelectedItem.ToString() is not providing the desired results.

XAML:

<Grid>

    <ListView x:Name="carList" VerticalAlignment="Top">
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Get Value" Click="GetValue_Click"/>
            </ContextMenu>
        </ListView.ContextMenu>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
                <GridViewColumn DisplayMemberBinding="{Binding Year}" Header="Year" Width="Auto"/>
            </GridView>
        </ListView.View>
    </ListView>

    <Button x:Name="generate" Content="Create List" Click="generate_Click" Margin="0,0,5,5" DockPanel.Dock="Top" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="88"/>
    <TextBox x:Name="textbox" HorizontalAlignment="Left" Height="23" Margin="5,0,0,5" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="386"/>

</Grid>

CS:

    public MainWindow()
    {
        InitializeComponent();
    }

    public class Car
    {
        public string Name { get; set; }
        public string Make { get; set; }
        public string Year { get; set; }
    }

    private void generate_Click(object sender, RoutedEventArgs e)
    {
        List<Car> cars = new List<Car>();

        int i = 0;
        string[] name = { "Sentra", "IS", "Camry" };
        string[] make = { "Nissan", "Lexus", "Toyota" };
        string[] year = { "2000", "2011", "2013" };

        foreach (string s in name)
        {
            cars.Add(new Car() { Name = name[i], Make = make[i], Year = year[i] });
            i++;
        }

        carList.ItemsSource = cars;
    }

    private void GetValue_Click(object sender, RoutedEventArgs e)
    {
        //Get text value from Car class, Name property
        //What is the correct code to access this?
        textbox.Text = carList.SelectedItem.ToString();
    }

Upvotes: 0

Views: 2194

Answers (1)

TrueEddie
TrueEddie

Reputation: 2233

You could add a property in the code behind for your selected Car:

public Car SelectedCar { get; set; }

And then set the selected item binding on the ListView:

<ListView SelectedItem="{Binding SelectedCar, Mode=TwoWay}" />

Then you can get everything about the selected vehicle from the SelectedCar property.

You made need to add DataContext = this; in your constructor also.

EDIT:

Full Code Behind:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
    public Car SelectedCar { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
    }

    public class Car
    {
        public string Name { get; set; }
        public string Make { get; set; }
        public string Year { get; set; }
    }

    private void generate_Click(object sender, RoutedEventArgs e)
    {
        List<Car> cars = new List<Car>();

        int i = 0;
        string[] name = { "Sentra", "IS", "Camry" };
        string[] make = { "Nissan", "Lexus", "Toyota" };
        string[] year = { "2000", "2011", "2013" };

        foreach (string s in name)
        {
            cars.Add(new Car() { Name = name[i], Make = make[i], Year = year[i] });
            i++;
        }

        carList.ItemsSource = cars;
    }

    private void GetValue_Click(object sender, RoutedEventArgs e)
    {
        textbox.Text = string.Format("{0} {1} {2}", SelectedCar.Year, SelectedCar.Make, SelectedCar.Name);
    }
}

Full XAML:

<Window xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <ListView x:Name="carList" VerticalAlignment="Top" SelectedItem="{Binding SelectedCar, Mode=TwoWay}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Get Value" Click="GetValue_Click"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Year}" Header="Year" Width="Auto"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button x:Name="generate" Content="Create List" Click="generate_Click" Margin="0,0,5,5" DockPanel.Dock="Top" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="88"/>
        <TextBox x:Name="textbox" HorizontalAlignment="Left" Height="23" Margin="5,0,0,5" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="386"/>

    </Grid>
</Window>

Upvotes: 2

Related Questions