user3488019
user3488019

Reputation: 51

How to Display information from a ListView into a TextBox using WPF

Hi I'm creating a car app to display a list of cars in a ListView and I want the users to be able to Click on the list item and it display in the TextBox. At the moment it works but it uses the ToString method and I cant format it at all, I would like to add a new line at the end of each record, so each property is on its own line, and generally have more control of how it displays. Is there a better way of doing it then what I have done. Ill include all my code so you have everything. I'm usually very average at asking questions so any advice with my question asking and my code would be greatly appreciated. Thanks..

class BaseCarClass
            {
                private string carBrand;
                private string carModel;
                private int yearModel;
                private int carPrice;



                //CarBrand Property
                public string CarBrand
                {
                    get
                    {
                        return carBrand;
                    }//end get
                    set
                    {
                        carBrand = value;
                    }//end set
                }//end property CarBrand


                public string CarModel
                {
                    get
                    {
                       return carModel;
                    }
                    set
                    {
                        carModel = value;
                    }
                }

                public int YearModel
                {
                    get
                    {
                        return yearModel;
                    }
                    set
                    {
                        yearModel = value;
                    }
                }

                public int CarPrice
                {
                    get
                    {
                        return carPrice;
                    }
                    set
                    {
                        carPrice = value;
                    }
                }
                //BaseClass Constructor
                public BaseCarClass(string car_brand, string car_model, int year_model, int car_price)
                {
                    CarBrand = car_brand;
                    CarModel = car_model;
                    YearModel = year_model;
                    CarPrice = car_price;
                }//end base class constructor

                public string space { get { return " "; } }

                public string GetCar
                {
                    get
                    {
                        return CarBrand+space+CarModel;
                    }
                }



                public override string ToString()
                {
                  return GetCar;
                }
            }



<Window x:Class="Assignment4ITC521.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Assignment4ITC521"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="550">
        <DockPanel>
            <Grid>
                <ListView Margin="10,10,152,10" Name="carListDisplay" SelectionChanged="carListDisplay_SelectionChanged">
                    <ListView.View>

                        <GridView>
                            <GridViewColumn Header="Brand" Width="120" DisplayMemberBinding="{Binding CarBrand}" />
                            <GridViewColumn Header="Model" Width="150" DisplayMemberBinding="{Binding CarModel}" />
                            <GridViewColumn Header="Year" Width="50" DisplayMemberBinding="{Binding YearModel}" />
                            <GridViewColumn Header="Price $" Width="50" DisplayMemberBinding="{Binding CarPrice}" />
                        </GridView>
                    </ListView.View>
                </ListView>

                <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="299" Margin="395,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="137" />

            </Grid>
        </DockPanel>
    </Window>





public partial class MainWindow : Window
        {
            private List<BaseCarClass> myCars = new List<BaseCarClass>();


     public MainWindow()
            {
                InitializeComponent();
                myCars.Add(new BaseCarClass("Ford", "Falcon", 1999, 2000));
                myCars.Add(new BaseCarClass("Holden", "Berina", 2008, 5000));
                myCars.Add(new BaseCarClass("Ford", "Mustang", 1967, 25000));

                //this.DataContext = myCars;
                carListDisplay.ItemsSource = myCars;



            }

            private void carListDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                string item = carListDisplay.Items[carListDisplay.SelectedIndex].ToString();
                textBox1.Text = (item);


            }
        }

Upvotes: 0

Views: 87

Answers (2)

asb
asb

Reputation: 833

From the current design you simple replace your space (" ") with "\n" to achieve the multiple records for each property. So you will be doing all your formatting withing GetCar property. If you need more control over each property, you can redesign your view to something like this. But that will depend on what your final requirements are.

Upvotes: 1

tgpdyk
tgpdyk

Reputation: 1233

You just need to bind the Text property of Textbox to the SelectedItem of ListView. No need for the handler.

Text="{Binding Path=SelectedItem, ElementName=carListDisplay}"

Good luck with your succeeding assignments.

Upvotes: 0

Related Questions