Thứ Sáu, 24 tháng 7, 2009

WPF: List Data Templates

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ả:



A21319952F68E79E_421_0[1]

Không có nhận xét nào:

Đăng nhận xét