0

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


            }
        }

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow that's was so obvious can't believe I missed it. Can you only put properties inside the ToString override or can I some how put all the formatting inside the ToString without using a separate property to create a space/newline?
There is no restriction that you can only put properties. You can (if you wish) put all the formatting logic in there.
All I did in the end was return string.Format("All the details and formatting in here") Thanks for the help though, I just missed the simple stuff after looking at it too long.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.