【发布时间】:2016-05-17 15:05:40
【问题描述】:
我知道这是基本的,但我正在从 vb.net 跳转到 C#,而我在 vb.net 中使用的方法似乎不起作用。
我创建了一个带有自定义类 Service 的 .dll。
在我的项目中,我正在使用 Service 实例填充 ObservableCollection。我想在 XAML (WPF) 中使用 DisplayMemberPath 在组合框中显示实例。
我的服务实例正在填充组合框,但每个项目的显示都是空白的;我只是得到一堆空白行可供选择。
我已经尝试过在类本身上实现和不实现 INotifyPropertyChanged,尽管我认为此时没有必要这样做,因为我仍然处于第 1 格。
这是我的代码:
<Grid>
<ComboBox Name="TopService"
VerticalAlignment="Top"
ItemsSource="{Binding}"
DisplayMemberPath="{Binding ServCode}"></ComboBox>
</Grid>
这是我的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Execute();
}
private void Execute()
{
SA.franchiseID = "HOL010";
ObservableCollection<Service> ocService = Service.InitializeServiceList();
TopService.DataContext = ocService;
}
}
以及类的代码(通过 .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;
}
}
}
【问题讨论】:
-
如果你收到
a bunch of blank lines,你可能想看看你的输出窗口,它通常会告诉你哪里出了问题。
标签: c# wpf xaml data-binding combobox