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

WPF: Binding to List Data (P2)

List Data Target

Trong phần trước chúng ta đã thấy cách để binding List oject, và navigate giữa các Items trong list. Trong rất nhiều trường hợp, chúng ta có nhu cầu binding toàn bộ object, ví dụ như binding vào Drop Down, Listbox, Grid…Để thực hiện việc binding toàn bộ Object hiện thời vào một control, chúng ta sử dụng {Binding} không có Path

<ListBox Grid.Column="1" Grid.Row="0" Name="lbPeople" 
    ItemsSource="{Binding}" 
    IsSynchronizedWithCurrentItem="True" />



Thuộc tính IsSynchronizedWithCurrentItem thiết lập là true để xác định rằng, khi chúng ta select item trong listbox thì nó sẽ thay đổi item hiện thời trong View



Fullcode:






<Window x:Class="ListDataBinding2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="List Data Binding" Height="300" Width="300" 
        xmlns:local="clr-namespace:ListDataBinding2" >
    <Window.Resources>
        <local:People x:Key="Hyperlogy">
            <local:Person x:Name="Nhat" FirstName="Nhat" LastName="Mai" FullName="Nhat Mai" />
            <local:Person x:Name="Quang" FirstName="Quang" LastName="Nguyen" FullName="Quang Nguyen" />
            <local:Person x:Name="Duc" FirstName="Duc" LastName="Le" FullName="Duc Le" />
        </local:People>
    </Window.Resources>
    <Grid Margin="10" Name="grid" DataContext="{StaticResource Hyperlogy}">
        <Grid.RowDefinitions>
            <RowDefinition Height="120*" />
            <RowDefinition Height="10*" />
            <RowDefinition Height="22*" />
            <RowDefinition Height="10*" />
            <RowDefinition Height="22*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="25*" />
            <RowDefinition Height="14.672*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="84*" />
            <ColumnDefinition Width="194*" />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="1" Grid.Row="0" Name="lbPeople" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" />
        <TextBox Grid.Column="1" Grid.Row="2" Name="txtFirstName" Text="{Binding Path=FirstName}" />
        <TextBox Grid.Column="1" Grid.Row="4" Name="txtLastName" Text="{Binding Path=LastName}"/>
        <TextBlock Grid.Row="2" Grid.Column="0">First Name:</TextBlock>
        <TextBlock Grid.Row="4" Grid.Column="0">Last Name:</TextBlock>
        <StackPanel Grid.Column="1" Grid.Row="6" Orientation="Horizontal">
            <Button>Btn1</Button>
            <Rectangle Width="10"></Rectangle>
            <Button>Btn2</Button>
        </StackPanel>
    </Grid>
</Window>


Kết quả:



A21319952F68E79E_418_0[1]



Display Members



Để thực hiện việc hiển thị property nào đó của Object lên listbox (Control) chúng ta sử dụng DisplayMemberPath="FullName"



Kết quả:



A21319952F68E79E_418_1[1]

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

Đăng nhận xét