wpf 组合框未正确显示“显示成员”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11961010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:58:39  来源:igfitidea点击:

Combobox not displaying 'Display Member' properly?

c#wpfxamldata-bindingcombobox

提问by djangojazz

Okay I am not understanding why WPF cannot display the 'Display Member' of a combobox item yet can do the 'selected value'. What is weird is I can explicitly create the display member but not get it back. In my example below I simplified the real world example that would be using ADO.NET to populate the itemsource but the concept is the same. I am generating a combobox that generates at runtime from a sql statement. I want to get the value for one thing but the display for something else. Is there some extra binding or customization I am missing or just the wrong member of .NET? I merely want to get 'Display of ...' to get selected back. If it is not possible that seems kind of weird. All I get back when I select is 'Value' so I know something is being lost in the transfer.

好吧,我不明白为什么 WPF 无法显示组合框项目的“显示成员”但可以执行“选定值”。奇怪的是我可以显式创建显示成员但不能取回它。在下面的示例中,我简化了使用 ADO.NET 填充 itemsource 的真实示例,但概念是相同的。我正在生成一个在运行时从 sql 语句生成的组合框。我想获得一件事的价值,但另一件事的显示。是否有一些额外的绑定或自定义我遗漏或只是 .NET 的错误成员?我只想让“...的显示”被选中。如果不可能,那看起来有点奇怪。当我选择“价值”时,我得到的只是“价值”,所以我知道在转移中丢失了一些东西。

Any ideas?

有任何想法吗?

XAML:

XAML:

<Window x:Class="WPFComboBoxTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
    <ComboBox x:Name="cmb" Width="100" Height="50"/>
    <TextBlock Height="100"/>
    <Button x:Name="btn" Content="Click" Click="btn_Click" Width="50"/>
    </StackPanel>

C# Code Behind:

背后的 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;

using System.Data;

namespace WPFComboBoxTest
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        BindComboBox(cmb);
    }

    public void BindComboBox(ComboBox cbx)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("Value", typeof(string));
        dt.Columns.Add("Id", typeof(int));

        dt.Rows.Add("Display of One", 1);
        dt.Rows.Add("Display of Two", 2);
        dt.Rows.Add("Display of Three", 3);
        dt.Rows.Add("Display of Four", 4);

        cbx.ItemsSource = dt.DefaultView;
        cbx.DisplayMemberPath = dt.Columns["Value"].ToString();
        cbx.SelectedValuePath = dt.Columns["Id"].ToString();
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Selected Value works: " + cmb.SelectedValue
            + "\n\nSelect Display does not though?: " + cmb.DisplayMemberPath
            );
    }
}
}

回答by H.B.

Calling ToStringon a column does not sound like a good idea to begin with, not sure how DataTablesare mapped but i would try

开始时调用ToString列听起来不是一个好主意,不确定如何DataTables映射,但我会尝试

cbx.DisplayMemberPath = "Value";
cbx.SelectedValuePath = "Id";

回答by djangojazz

(facepalm) it was pretty simple. cmb.DisplayMemberPath needs to be cmb.Text. The 'Text' was the property I needed to display the display member shown to the user.

(脸掌)这很简单。cmb.DisplayMemberPath 需要是 cmb.Text。'Text' 是我需要向用户显示显示成员的属性。

回答by Mubarak Shaikh

Change Your btn_Clickevent Data as Following:

更改您的btn_Click事件数据如下:

private void btn_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Selected Value works: " + cmb.SelectedValue
            + "\n\nSelect Display does not though?: " + cmb.Text
    );
}

I think working fine

我认为工作正常

回答by José Roberto Cuello Alcaraz

Try using:

尝试使用:

cbx.DisplayMemberPath = "[Value]";
cbx.SelectedValuePath = "[Id]";

It works for me!

这个对我有用!