iblog 5

苹果有iphone,我有iblog

导航

WPF条目类控件的IsSynchronizedWithCurrentItem属性

Posted on 2012-07-02 17:41  cbtech  阅读(1549)  评论(0编辑  收藏  举报

 

原文出处:http://www.bianceng.cn/Programming/net/201010/19512_2.htm

 

为了使用 Personal 和 PersonalList,我们引入了一个 CLR Namespace。

在主从结构 (Master-Detail) 显示中,我们通常要实现 "选择项跟踪" 功能,也就是说当我们选中主表的某个记录时,其他细节控件要同步刷新该记录的细节内容。

 

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:Learn.WPF"
  Title="Window1">
  <Window.Resources>
    <my:PersonalList x:Key="personals" >
      <my:Personal Name="Tom" Age="10" Sex="Male" />
      <my:Personal Name="Mary" Age="15" Sex="Female" />
      <my:Personal Name="Jack" Age="12" Sex="Male" />
    </my:PersonalList>
  </Window.Resources>
  <Grid>
    <StackPanel>
      <ListBox x:Name="listbox1"
        ItemsSource="{Binding Source={StaticResource personals}}"
        DisplayMemberPath="Name"
        IsSynchronizedWithCurrentItem="True">
      </ListBox>

      <Label x:Name="lblName" Content="{Binding Source={StaticResource personals}, Path=Name}" />
      <Label x:Name="lblAge" Content="{Binding Source={StaticResource personals}, Path=Age}"/>
      <Label x:Name="lblSex" Content="{Binding Source={StaticResource personals}, Path=Sex}"/>
    </StackPanel>
  </Grid>
</Window>

 

我们将 ListBox.IsSynchronizedWithCurrentItem 置为 True,这样当我们改变 ListBox 选择项时,下面三个标签会自动同步变更为被选中记录的信息。

 

 

 

当然,我们还可以使用多个 ListBox,就像真正的主从表显示那样相互影响。

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:Learn.WPF"
  Title="Window1">
  <Window.Resources>
    <my:PersonalList x:Key="personals" >
      <my:Personal Name="Tom" Age="10" Sex="Male" />
      <my:Personal Name="Mary" Age="15" Sex="Female" />
      <my:Personal Name="Jack" Age="12" Sex="Male" />
    </my:PersonalList>
  </Window.Resources>
  <Grid>
    <StackPanel>
      <ListBox x:Name="listbox1"
        ItemsSource="{Binding Source={StaticResource personals}}"
        DisplayMemberPath="Name"
        IsSynchronizedWithCurrentItem="True">
      </ListBox>

      <ListBox x:Name="listbox2"
        ItemsSource="{Binding Source={StaticResource personals}}"
        DisplayMemberPath="Age"
        IsSynchronizedWithCurrentItem="True">
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

 

有一点需要说明,IsSynchronizedWithCurrentItem 不支持多项选择同步。