KimhillZhang  

/// <summary>
    /// 集合绑定.xaml 的交互逻辑
    /// </summary>
    public partial class 集合绑定 : Window
    {
        public 集合绑定()
        {
            InitializeComponent();
            AddressList address = new AddressList()
            {
                new uAddress(){ city = "福州", area = "仓山区"},
                new uAddress(){ city="漳平", area="东环路"}
            };
            listbox1.DataContext = address;
        }
    }
    public class uAddress
    {
        public string city { get; set; }
        public string area { get; set; }
    }
    public class AddressList : ObservableCollection<uAddress>
    { }

 

xmlns:my="clr-namespace:WpfApplication5"

 

<ListBox x:Name="listbox1" ItemsSource="{Binding Path=.}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type my:uAddress}">
                    <TextBlock>
                        <TextBlock.Text>
                            <Binding Path="city"/>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

 

 

 

<Window.Resources>
        <my:AddressList x:Key="addresses">
           <my:uAddress area="鼓楼区" city="福州"/>
           <my:uAddress area="晋安区" city="福州"/>
           <my:uAddress area="晋安区" city="福州"/>
           <my:uAddress area="晋安区" city="福州"/>
           <my:uAddress area="晋安区" city="福州"/>
        </my:AddressList>
    </Window.Resources>

<ListBox Height="100" x:Name="listbox2" ItemsSource="{StaticResource ResourceKey=addresses}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type my:uAddress}">
                    <TextBlock>
                        <TextBlock.Text>
                            <Binding Path="area"/>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

posted on 2012-03-28 09:01  KimhillZhang  阅读(1374)  评论(0编辑  收藏  举报