40

Trying to learn how to bind objects to various types of controls. In this instance, I want to get sample data in my object to appear in ComboBox. The code runs but what appears instead of values (David, Helen, Joe) is text "TheProtect.UserControls.Client")

XAML: (ucDataBindingObject.xaml)

<UserControl x:Class="TheProject.UserControls.ucDataBindingObject"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             Width="Auto"
             Height="Auto"
             mc:Ignorable="d">

    <Grid Width="130"
          Height="240"
          Margin="0">

            <ComboBox Width="310"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      ItemsSource="{Binding Path=Clients}" />
    </Grid>
</UserControl>

C#: ucDataBindingObject.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
namespace TheProject.UserControls
{
    public partial class ucDataBindingObject : UserControl
    {

        public List<Client> Clients { get; set; }


        public ucDataBindingObject()
        {
            Clients = new List<Client>();
            Clients.Add(new Client(1, "David")); // sample data
            Clients.Add(new Client(2, "Helen"));
            Clients.Add(new Client(3, "Joe"));


            InitializeComponent();
            this.DataContext = this;
        }
    }

C# Client.cs

using System;
using System.Linq;

namespace TheProject.UserControls
{
    public class Client
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public Client(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
    }
}
4

3 回答 3

80

有几种方法可以告诉框架要显示什么

1)DisplayMemberPath在 ComboBox 上使用(这将显示命名属性):

<ComboBox ItemsSource="{Binding Path=Clients}" 
          DisplayMemberPath="Name"
/>

2)ItemTemplate在组合框上设置。这就像#1,除了允许您定义要显示的模板,而不仅仅是一个属性:

<ComboBox ItemsSource="{Binding Path=Clients}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Green" BorderThickness="1" Padding="5">
                <TextBlock Text="{Binding Path=Name,StringFormat='Name: {0}'}" />
            </Border>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

3)添加一个ToString()覆盖源类。如果您总是想为给定的类显示相同的字符串,这很有用。(请注意,默认ToString()只是类类型名称,这就是您看到“TheProtect.UserControls.Client”的原因。)

public class Client
{
    // ...

    public override string ToString()
    {
        return string.Format("{0} ({1})", Name, ID);
    }
}

4) 添加一个DataTemplate到 XAML 资源。这对于将给定的类类型与更复杂或风格化的模板相关联很有用。

<UserControl xmlns:local="clr-namespace:TheProject.UserControls">
    <UserControl.Resources>
        <DataTemplate DataType="local:Client">
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </UserControl.Resources>

    // ...

</UserControl>    
于 2013-09-28T21:07:37.433 回答
10

DisplayMemberPath中,给出要在组合框中显示的属性的名称。在SelectedValuePath中,给出您要选择的属性的名称。当您执行 aComboBox.SelectedValue时,您将获得此属性的值。

于 2013-09-29T12:48:54.703 回答
0

尝试从组合框中获取所选值返回 System.Data.Entity.DynamicProxies.x

 private void Button_Click(object sender, RoutedEventArgs e){
         string _scanner0 = int.Parse(mycmb.SelectedValue.ToString());
         string _scanner1 = mycbr.SelectedItem.ToString();
         string _scanner2 = mycbr.SelectedValuePath.ToString();
         string _scanner3 = mycbr.text.ToString();
     }

所有这些都返回 System.Data.Entity.DynamicProxies.x 我该怎么办?

于 2019-11-07T17:56:22.550 回答