简体   繁体   English

Combobox没有正确显示“显示会员”?

[英]Combobox not displaying 'Display Member' properly?

Okay I am not understanding why WPF cannot display the 'Display Member' of a combobox item yet can do the 'selected value'. 好吧,我不明白为什么WPF无法显示组合框项目的“显示成员”但可以执行“选定值”。 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. 在下面的示例中,我简化了将使用ADO.NET填充itemsource的真实示例,但概念是相同的。 I am generating a combobox that generates at runtime from a sql statement. 我正在生成一个在运行时从sql语句生成的组合框。 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? 是否有一些我缺少的额外绑定或定制或只是.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. 我选择的所有东西都是'价值',所以我知道转移中丢失了一些东西。

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
            );
    }
}
}

Calling ToString on a column does not sound like a good idea to begin with, not sure how DataTables are mapped but i would try 在列上调用ToString听起来不是一个好主意,不确定DataTables是如何映射但我会尝试

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

(facepalm) it was pretty simple. (facepalm)这很简单。 cmb.DisplayMemberPath needs to be cmb.Text. cmb.DisplayMemberPath需要是cmb.Text。 The 'Text' was the property I needed to display the display member shown to the user. 'Text'是显示向用户显示的显示成员所需的属性。

Change Your btn_Click event 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 我认为工作正常

Try using: 尝试使用:

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

It works for me! 这个对我有用!

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

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