ListBox之类控件的Item项显示对象的两个属性

   wpf项目中,ListBox绑定对象集合,ListBoxItem要显示对象的两个属性,例如:显示员工的工号和姓名。

之前我的做法是在Employee员工类中添加一个"NumAndName",属性,给员工对象的工号属性赋值、姓名属性赋值时,同时给“NumAndName”属性

赋值为“工号”+“姓名”两个属性拼接的字符串,ListBox绑定员工的集合,ListBox的Item绑定"NumAndName"属性,这样就同时显示了工号和姓名。

    今天,发现了一个简单的方法,即:在Employee类中重载ToString()方法,核心代码如下:

                  

    public class Employee
    {
        /// <summary>
        /// 重载ToString()方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Num+"  "+this.Name;
        }

      //……

下面是demo的完整代码:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="225">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListBox x:Name="lbx" SelectionMode="Single"/>
        <Button Grid.Row="1" Content="Show" Click="Button_Click"/>
    </Grid>
</Window>
View Code

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Employee> list = new List<Employee>
            { 
               new Employee{Num=9527,Name="张三",Sex="男"},
               new Employee{Num=1086,Name="春丽",Sex="女"},
               new Employee{Num=1001,Name="王五",Sex="男"}
            };
            lbx.ItemsSource = list;
        }
        /// <summary>
        /// Show出lbx的选中项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (lbx.SelectedIndex > -1)
            {
                Employee employee = lbx.SelectedItem as Employee;
                string msg = string.Format("工号:{0},
姓名:{1},
性别:{2}", employee.Num, employee.Name, employee.Sex);
                MessageBox.Show(msg);
            }
            else
            {
                MessageBox.Show("未选中任何项!");
            }
        }
    }
    public class Employee
    {
        /// <summary>
        /// 重载ToString()方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Num+"  "+this.Name;
        }
        private string name;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int num;
        /// <summary>
        /// 工号
        /// </summary>
        public int Num
        {
            get { return num; }
            set { num = value; }
        }
        private string sex;
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        
    }
}
View Code


运行效果:

选中ListBox的某项后,按"Show"按钮,Show出春丽的工号、姓名、性别:

原文地址:https://www.cnblogs.com/527289276qq/p/4375940.html