简体   繁体   English

使用DisplayMemberPath将自定义类绑定到WPF ComboBox

[英]Data Binding Custom Class to WPF ComboBox Using DisplayMemberPath

I know this is basic, but I'm making the jump from vb.net to C#, and the approach I was using in vb.net doesn't seem to be working. 我知道这是基本的,但我正在从vb.net跳转到C#,而我在vb.net中使用的方法似乎并不起作用。 I've created a .dll with a custom class Service. 我用自定义类服务创建了一个.dll。
In my project, I'm populating an ObservableCollection with instances of Service. 在我的项目中,我正在使用Service实例填充ObservableCollection。 I want to display the instances in a combobox using DisplayMemberPath in XAML (WPF). 我想在XAML(WPF)中使用DisplayMemberPath在组合框中显示实例。

My instances of Service are populating the ComboBox, but the display for each item is blank; 我的服务实例正在填充ComboBox,但每个项目的显示都是空白的; I'm just getting a bunch of blank lines to choose from. 我只是得到一堆空行可供选择。

I've tried this with and without implementing INotifyPropertyChanged on the class itself, although I don't think it should be necessary at this point since I'm still pretty much at square 1. 我已经尝试了这个,有没有在类本身上实现INotifyPropertyChanged,虽然我不认为此时应该是必要的,因为我仍然非常在第1方。

Here's my code: 这是我的代码:

      <Grid>
        <ComboBox Name="TopService"
                        VerticalAlignment="Top"
                        ItemsSource="{Binding}"
                        DisplayMemberPath="{Binding ServCode}"></ComboBox>
      </Grid>

And here's my code behind: 这是我的代码背后:

        public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Execute();
    }
    private void Execute()
    {
        SA.franchiseID = "HOL010";
        ObservableCollection<Service> ocService = Service.InitializeServiceList();
        TopService.DataContext = ocService;
    }
}

And the code for the class (referenced via .dll) 和类的代码(通过.dll引用)

    public class Service : INotifyPropertyChanged
{
    #region INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propertyName)
    {
        if (this.PropertyChanged != null)
        { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    }
    #endregion
    private string servCode;
    public string ServCode
    {
        get { return servCode; }
        set { servCode = value; Notify("ServCode"); }
    }
    public string programCode = "";
    public int roundNum = 0;
    public static ObservableCollection<Service> InitializeServiceList()
    {
        ObservableCollection<Service> oc = new ObservableCollection<Service>();
        using (SA s = new SA())
        {
            s.SqlString = @"select 
ps.serv_code
,pc.prgm_code
,ps.round
from prgmserv as ps 
inner join prgmcd as pc on pc.progdefid = ps.progdefid
where pc.available = 1";
            s.reader = s.command.ExecuteReader();
            Service newService;
            while (s.reader.Read())
            {
                newService = new Service();
                newService.servCode = s.reader["serv_code"].ToString();
                newService.programCode = s.reader["prgm_code"].ToString();
                newService.roundNum = Convert.ToInt32(s.reader["round"].ToString());
                oc.Add(newService);
                newService = null;
            }
            return oc;
        }
    }
}

DisplayMemberPath is a string . DisplayMemberPath是一个字符串 You don't give it a binding to a property; 你没有给它绑定一个属性; you just give it a path to a property, which it then looks up by reflection. 你只需给它一个属性的路径,然后通过反射查找它。

Try this: 尝试这个:

DisplayMemberPath="ServCode"

What you were doing would make it use the value of ServCode as a DisplayMemberPath ; 你在做什么会使它使用ServCode作为DisplayMemberPath ; if ServCode is 12 , it would look for a property named 12 on each item in the combobox -- not your intent, I'm sure. 如果ServCode是12 ,它会在组合框中的每个项目上寻找一个名为12的属性 - 不是你的意图,我敢肯定。

I have also come to realize while attempting to bind items to an ItemsControl, that the difference between a Field and a Property become very important. 我在尝试将项目绑定到ItemsControl时也意识到,Field和Property之间的区别变得非常重要。 I have been trying to bind to Fields, which does not work. 我一直试图绑定到Fields,这是行不通的。 This is technically not different than in VB.net. 这在技术上与VB.net没有什么不同。 But an implicitly defined Property in VB.net looks very much like a Field in C#. 但是VB.net中隐式定义的属性看起来非常像C#中的Field。 All that said, I believe I'm ready to go conquer the world now! 所有这一切,我相信我现在准备好征服世界了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM