0

如何从ListView选定的单元格中获取文本?

我有一个单击即可ListView创建列表。Button我想通过右键单击检索 Car 列的文本值。我不知道正确的命令是什么。SelectedItem.ToString()没有提供预期的结果。

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();
    }
4

1 回答 1

2

您可以在代码中为您选择的汽车添加一个属性:

public Car SelectedCar { get; set; }

然后在 ListView 上设置选中项绑定:

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

然后,您可以从 SelectedCar 属性中获取有关所选车辆的所有信息。

您需要添加DataContext = this; 在你的构造函数中。

编辑:

完整代码背后:

/// <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);
    }
}

完整的 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>
于 2013-09-16T18:10:46.560 回答