开发学院WEB开发ASP.NET WPF学习笔记12. Binding (5) 阅读

WPF学习笔记12. Binding (5)

 2010-10-11 16:35:20 来源:本站整理   
核心提示:接下来,我们尝试绑定到一个方法上,WPF学习笔记12. Binding (5)(4),Window1.xaml.csclassPersonalList:ObservableCollection<Personal>{publicPersonalListGetPersonals(){this.Add(newPe

接下来,我们尝试绑定到一个方法上。

Window1.xaml.cs

class PersonalList : ObservableCollection<Personal>
{
  public PersonalList GetPersonals()
  {
    this.Add(new Personal("Tom", 15, Sex.Male));
    this.Add(new Personal("Mary", 11, Sex.Female));
    this.Add(new Personal("Jack", 13, Sex.Male));
    return this;
  }
}
public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
  }
}

Window1.xaml

<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"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  Title="Window1">
  <Window.Resources>

    <ObjectDataProvider x:Key="personals" ObjectType="{x:Type my:PersonalList}" 
      MethodName="GetPersonals" />

  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personals}">
      <ListBox x:Name="listbox1" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Path=Name}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding Path=Age}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding Path=Sex}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

和构造方法参数一样,我们也可以向方法提供参数。

Window1.xaml.cs

class PersonalList : ObservableCollection<Personal>
{
  public IEnumerable<Personal> GetPersonals(int top)
  {
    this.Add(new Personal("Tom", 15, Sex.Male));
    this.Add(new Personal("Mary", 11, Sex.Female));
    this.Add(new Personal("Jack", 13, Sex.Male));
    return this.Take(top);
  }
}

Window1.xaml

<ObjectDataProvider x:Key="personals" ObjectType="{x:Type my:PersonalList}" MethodName="GetPersonals">
  <ObjectDataProvider.MethodParameters>
    <sys:Int32>2</sys:Int32>
  </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

上一页  1 2 3 4 

Tags:WPF 学习 笔记

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接