-3

I have a basic project which is meant to add some Client info to a listbox. the user enters the values in the fields and then clicks on the Add Client button which should be adding the names to the list box, 'lstClient'. My Show Client button works as expected, bringing up the window with each client's details when I select a client from the list box.

// defining the Client class

namespace WpfApp_Employment_Help
{
    public partial class Client

    {
        private static int nextID = 0;
        public string ID { get; private set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Location { get; }

        public bool IDed { get; private set; }

        public Client(string n, string p, string e, string l)
        {
            Name = n;
            Phone = p;
            Email = e;
            Location = l;

        }

        public void AssignID()
        {
            if (!IDed)
            {
                ID = nextID.ToString("D4");
                nextID++;
                IDed = true;
            }
        }

// the Main Window

namespace WpfApp_Employment_Help
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //  client list

        List<Client> ClientList = new List<Client>();
        public MainWindow()
        {
            InitializeComponent();
        }

        // method to select location via radio button
        public string GetSelectedLocation()
        {
            string selected = string.Empty;
           if (RBLocE.IsChecked == true) { selected = "Edinburgh"; }
           else if (RBLocG.IsChecked == true) { selected = "Glasgow"; }
           else if (RBLocO.IsChecked == true) { selected = "Other"; }

            return selected;
        }


        // method to create a new client on click
        private void newClient(object sender, RoutedEventArgs e)
        {
            Client c = new Client(boxClientName.Text, boxClientPhone.Text, boxClientEmail.Text, GetSelectedLocation());
            boxClientName.Clear();
            boxClientPhone.Clear();
            boxClientEmail.Clear();
            ClientList.Add(c);
            lstClients.ItemsSource = null;
            lstClients.ItemsSource = ClientList;
        }

        // method to id selected client
        private void AssignID(object sender, RoutedEventArgs e)
        {
            Client c = lstClients.SelectedItem as Client;
            if (c != null)
            {
                c.AssignID();
            }
            lstClients.ItemsSource = null;
            lstClients.ItemsSource = ClientList;
        }

        // method to show details of selected client
        private void showDetails(object sender, RoutedEventArgs e)
        {
            Client c = lstClients.SelectedItem as Client;
            if (c != null)
            {
                ClientDetails w = new ClientDetails(c);
                w.Show();
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
                    }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void ListBoxVac_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void TextBoxClient_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void BtnGenShortlist_Click(object sender, RoutedEventArgs e)
        {

        }

        private void AssignID(object sender, ContextMenuEventArgs e)
        {

        }
    }
}

However, when I run the application, it displays the name of my project, 'WpfApp_Employment_Help' in my listbox. How can I display the client name?

I'll also need to remove a client from the list later (when I figure out how to add them first!) thank you.

enter image description here

Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • Does this answer your question? [WPF Combobox DisplayMemberPath](https://stackoverflow.com/questions/1460612/wpf-combobox-displaymemberpath) – Tarazed Dec 15 '22 at 18:56
  • 2
    This is just advice and not really a solution, but it'll help you find a simple solution: [Learn how to use MVVM in WPF](https://stackoverflow.com/a/2034333/5593150). Not only is this the _right_ way to use WPF, it is also the better, more scalable way. – Max Play Dec 15 '22 at 19:03
  • DisplayMemberPath does not answer my question. I have a similar solution given in class, and it does not involve changing anything in change DisplayPath to add the items to the listbox. – Bluetail Dec 15 '22 at 19:43

1 Answers1

1

Set the DisplayMemberPath to the name of the property of the Client class that you want to display in the ListBox:

<ListBox x:Name="lstClients" DisplayMemberPath="Name" />

Or use an ItemTemplate:

<ListBox x:Name="lstClients">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

You could also set the DisplayMemberPath programmcatically:

private void newClient(object sender, RoutedEventArgs e)
{
    Client c = new Client(boxClientName.Text, boxClientPhone.Text, boxClientEmail.Text, GetSelectedLocation());
    boxClientName.Clear();
    boxClientPhone.Clear();
    boxClientEmail.Clear();
    ClientList.Add(c);
    lstClients.ItemsSource = null;
    lstClients.ItemsSource = ClientList;
    lstClients.DisplayMemberPath = nameof(Client.Name); // <--
}

Either way, you need to define how you want your Client object to be displayed in the view.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • thank you v much for a detailed answer. – Bluetail Dec 15 '22 at 20:15
  • would you know how to remove a selected item? i have tried public void RemoveClient(Client c) { if (Client c = lstClients.SelectedItem as Client) { ClientList.Remove(c); } } it does not work. – Bluetail Dec 15 '22 at 20:37
  • Please ask a new question if you have another issue. – mm8 Dec 15 '22 at 20:37