【问题标题】:Combobox not displaying 'Display Member' properly?组合框未正确显示“显示成员”?
【发布时间】:2012-08-14 21:27:57
【问题描述】:

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

有什么想法吗?

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# 代码背后:

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

【问题讨论】:

  • (facepalm) 这很简单。 cmb.DisplayMemberPath 需要为 cmb.Text。 “文本”是我需要向用户显示显示成员的属性。

标签: c# wpf xaml data-binding combobox


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    (facepalm) 这很简单。 cmb.DisplayMemberPath 需要为 cmb.Text。 “文本”是我需要向用户显示显示成员的属性。

    【讨论】:

      【解决方案3】:

      更改您的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
          );
      }
      

      我认为工作正常

      【讨论】:

        【解决方案4】:

        尝试使用:

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

        它对我有用!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 2020-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-09
          相关资源
          最近更新 更多