WPF:如何将对象绑定(bind)到组合框

标签 wpf xaml object data-binding combobox

尝试学习如何将对象绑定(bind)到各种类型的控件。在这种情况下,我想让我的对象中的示例数据出现在 ComboBox 中。代码运行,但出现的不是值(大卫、海伦、乔)是文本“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# 客户端.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;
        }
    }
}

最佳答案

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

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>    

关于WPF:如何将对象绑定(bind)到组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19071664/

相关文章:

c# - Windows Presentation Foundation(WPF)项目不支持AppCommand

C# WPF 标签可见性异常

c# - 如何访问 RadGridView 单元格内的子文本框

perl 中的对象数组?

php - 在整个应用程序中使用对象

wpf - 绑定(bind)到 DependencyProperty 不起作用

c# - 如何避免复杂屏幕上的数据绑定(bind)/事件 hell ?

json - 从组件中的Service解析JSON数组对象

wpf mvvm 加速键绑定(bind)

xaml - 如何绑定(bind)样式资源字典中的颜色?