WPF: Use ype and wpfdatatemplate in DataTemplate

Source: Internet
Author: User

WPF: Use ype and wpfdatatemplate in DataTemplate

The DataType function in DataTemplate is actually similar to the TargetType in Style.

If the TargetType is used in the Style, if the Key of the Style is not defined, the Style will affect the Style of all the TargetType controls in the region where the Style is located.

Similarly, after DataTemplate is used, if the DataTemplate Key is not defined, the DataTemplate will be applied to the region where it is located, and all controls that use this DataTemplate as the data source.

I wrote a small example to demonstrate this effect.

Define Model, Person:

    public class Person    {        public string Name        {            get;            set;        }        public int Age        {            get;            set;        }    }

Define ViewModel:

Public class MainViewModel {public ObservableCollection <Person> AllPerson {get; set;} public MainViewModel () {AllPerson = new ObservableCollection <Person> {new Person {Name = "James ", age = 18}, new Person {Name = "Li Si", Age = 28 }};}}

DataTemplate is defined and DataType is used:

    <Window.Resources>        <DataTemplate DataType="{x:Type local:Person}">            <StackPanel Orientation="Horizontal">                <TextBlock Text="{Binding Name}" />                <TextBlock Text="{Binding Age}" />            </StackPanel>        </DataTemplate>    </Window.Resources>

The ItemTemplate of ItemControl uses the emplate defined above:

        <ItemsControl ItemsSource="{Binding AllPerson}">            <ItemsControl.ItemsPanel>                <ItemsPanelTemplate>                    <StackPanel Orientation="Vertical" />                </ItemsPanelTemplate>            </ItemsControl.ItemsPanel>        </ItemsControl>

Because the ItemControl Item data source is Person and a Person emplate with Person as DataType exists, the ItemTemplate of ItemControl automatically applies the previously defined DataTemplate.

The running effect is as follows:

Related Article

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.