basquang™ on clouds

July 24, 2009

WPF: List Data Templates

Filed under: WPF — basquang @ 3:22 AM

Khi binding to List Object, đôi khi chúng ta cần show nhiều property của Object lên, chúng ta sử dụng data template. Trong ví dụng trước, Listbox chỉ display FullName property, chúng ta sẽ customize sự hiển thị này sử dụng data template.

<ListBox Grid.Column="1" Grid.Row="0" Name="lbPeople" 
                 ItemsSource="{Binding}" 
                 IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock Text="{Binding Path=FirstName}"/>   
                        [Fullname: <TextBlock Text="{Binding Path=FullName}"/>]
                    </TextBlock>                    
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Để sử dụng Data Template chúng ta cần loại bỏ thuộc tính DisplayMemberPath

Kết quả:

image

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: