2013-09-28 329 views
26

Cố gắng tìm hiểu cách liên kết đối tượng với các loại điều khiển khác nhau. Trong trường hợp này, tôi muốn lấy dữ liệu mẫu trong đối tượng của mình để xuất hiện trong ComboBox. Mã chạy nhưng những gì xuất hiện thay vì giá trị (David, Helen, Joe) là văn bản "TheProtect.UserControls.Client")WPF: Cách liên kết đối tượng với ComboBox

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

Trả lời

56

có một số cách để nói với khuôn khổ những gì để hiển thị

1) Sử dụng DisplayMemberPath trên ComboBox (điều này sẽ hiển thị tên tài sản):

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

2) Đặt ItemTemplate trên ComboBox. Điều này giống như # 1, ngoại trừ cho phép bạn xác định một mẫu để hiển thị, thay vì chỉ là một thuộc tính:

<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) Thêm ToString() ghi đè lên lớp nguồn. Hữu ích nếu bạn luôn muốn hiển thị cùng một chuỗi cho một lớp nhất định. (Lưu ý rằng mặc định ToString() chỉ là tên kiểu lớp, đó là lý do tại sao bạn thấy "TheProtect.UserControls.Client".)

public class Client 
{ 
    // ... 

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

4) Thêm một DataTemplate đến tài nguyên XAML. Điều này rất hữu ích khi liên kết một loại lớp nhất định với một mẫu phức tạp hơn hoặc được cách điệu hơn.

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

    // ... 

</UserControl>  
+0

Cảm ơn! Rõ ràng câu trả lời và làm việc tốt của nó! –

8

Trong DisplayMemberPath, cung cấp cho các tên của thuộc tính mà bạn muốn hiển thị trong ComboBox. Trong SelectedValuePath, đặt tên của thuộc tính bạn muốn chọn. Khi bạn thực hiện một số ComboBox.SelectedValue, bạn sẽ nhận được giá trị của thuộc tính này.