Home > OS >  Adding names to a list box problem with C# in WPF
Adding names to a list box problem with C# in WPF

Time:12-16

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

CodePudding user response:

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.

  • Related