WPF Learning Notes

Source: Internet
Author: User

6. Data template Data templates provide a great deal of flexibility for presenting data, and we continue to look at its capabilities in the previous example.

<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= "ten" sex= "Male"/>
<my:personal name= "Mary" age= "sex=" Female "/>"
<my:personal name= "Jack" age= "sex=" Male "/>"
</my:PersonalList>
</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&gt, </TextBlock>
<textblock text= "{Binding path=age}"/>
<TextBlock>,</TextBlock>
<textblock text= "{Binding path=sex}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>

Obviously, using ListBox.ItemTemplate.DataTemplate, we can display data source objects in more complex and richer ways, instead of just displaying a single property through Path. But normally, we would define a data template in a resource, like HTML/CSS, for reuse in multiple places.

<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= "ten" sex= "Male"/>
<my:personal name= "Mary" age= "sex=" Female "/>"
<my:personal name= "Jack" age= "sex=" Male "/>"
</my:PersonalList>

<datatemplate x:key= "MyData" >
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding path=name}"/>
<TextBlock>,</TextBlock>
<textblock text= "{Binding path=age}"/>
<TextBlock>,</TextBlock>
<textblock text= "{Binding path=sex}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<stackpanel datacontext= "{StaticResource personals}" >
<listbox x:name= "ListBox1" Itemssource= "{Binding}"
Itemtemplate= "{StaticResource MyData}" >
</ListBox>
</StackPanel>
</Grid>
</Window>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.