basquang™ on clouds

July 24, 2009

WPF: Binding to List Data (P2)

Filed under: WPF — basquang @ 3:03 AM

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

image

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

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: