c# - 使用 DisplayMemberPath 将自定义类数据绑定(bind)到 WPF ComboBox

标签 c# wpf xaml data-binding combobox

我知道这是基本的,但我正在从 vb.net 跳到 C#,我在 vb.net 中使用的方法似乎不起作用。 我创建了一个带有自定义类服务的 .dll。
在我的项目中,我使用服务实例填充 ObservableCollection。我想在 XAML (WPF) 中使用 DisplayMemberPath 在组合框中显示实例。

我的服务实例正在填充组合框,但每个项目的显示都是空白的;我只是得到一堆空白行可供选择。

我已经在类本身上实现和不实现 INotifyPropertyChanged 的​​情况下进行了尝试,尽管我认为此时没有必要,因为我仍然处于方 block 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;
        }
    }
}

最佳答案

DisplayMemberPath is a string .您不给它绑定(bind)属性;你只需给它一个属性的路径,然后它通过反射查找。

试试这个:

DisplayMemberPath="ServCode"

您所做的会使它使用ServCode 作为DisplayMemberPath;如果 ServCode 是 12,它将在组合框中的每个项目上查找名为 12 的属性——我敢肯定这不是您的意图。

关于c# - 使用 DisplayMemberPath 将自定义类数据绑定(bind)到 WPF ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37279870/

相关文章:

c# - C#混淆中字符串加密的安全性

c# - 如何使用 azure 服务总线 5.0.0 在 C# azure 函数中手动处理消息完成

wpf - WPF中如何显示多层控件?

c# - .net 中的 PayPal 定期付款

c# - 将 `DataGrid` 替换为 `GridView` 时我们必须更改哪些内容?

wpf - 指针 WPF 控件

wpf - 如何设置文本 block 自动调整大小以适应 TreeView 的宽度?

c# - 如何将 Canvas 绑定(bind)到矩形列表

xaml - ISupportIncrementalLoading 结合 VariableSizedWrapGrid

c# - TreeView 的选定项目