WPF: Template, Converter

Source: Internet
Author: User

Control
|
+ ----ContentControlCorresponding ControlTemplate
|
| + ---- ListBoxItem
|
| + ---- HeaderedContentControl
|
| + ---- TabItem
|
+ ----ItemsControlCorresponding ItemTemplate
|
+ ---- TreeView
|
+ ---- MenuBase
|
+ ---- HeaderedItemsControl
|
| + ---- MenuItem
|
| + ---- TreeViewItem
|
+ ---- Selector
|
+ ---- TabControl
|
+ ---- ListBox

See: http://www.cnblogs.com/larrysunday/articles/1212998.html

The following code is similar to: button. Template = new ControlTemplate ()

<Style TargetType="Button">  <Setter Property="OverridesDefaultStyle" Value="True"/>  <Setter Property="Template">    <Setter.Value>      <ControlTemplate TargetType="Button">        <Grid>          <Ellipse Fill="{TemplateBinding Background}"/>          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>        </Grid>      </ControlTemplate>    </Setter.Value>  </Setter></Style>

========================================================== ========================================================== ======

Converter

First write the Converter class

[ValueConversion (typeof (Color), typeof (SolidColorBrush)]
Public class ColorBrushConverter: IValueConverter
{
Public objectConvert(Object value, Type targetType, object parameter, System. Globalization. CultureInfo culture)
{
Color color = (Color) value;
Return new SolidColorBrush (color );
}
Public objectConvertBack(Object value, Type targetType, object parameter, System. Globalization. CultureInfo culture)
{
Return null;
}
}

Then we can define a ColorBrushConverter instance in the resource (cvtns is a custom namespace and the Assembly of the ColorBrushConverter class is introduced ).

<Application. Resources>
<Cvtns: ColorBrushConverter x: Key = "ColorToBrush"/>
</Application. Resources>

Finally, use this custom type converter:

<DataTemplate DataType = "{x: Type Color}">
<Rectangle Height = "25" Width = "25" Fill = "{Binding Converter = {StaticResource ColorToBrush}"/>
</DataTemplate>

 

========================================================== ===

As you can tell from the above figure, there are 4 kinds of Templates available within WPF.

1.ControlTemplate-You use this, when you want to completely redefine the visual appearance of any control. say, you don't want a radiobutton to look like a radiobutton-you want it to look like a smiley instead. smiling means Checked, and Frowning means Unchecked. you cocould easily acheive this using ControlTemplate.

2.ItemsPanelTemplate-Is a rather simple kind of template, it lets you control the appearance of the "ItemsPanel" property defined by "ItemsControl" on elements such as ListBox or ComboBox.

3.DataTemplate-Is probably the most common kind of template you will use. it lets you change how "Content" is rendered on any control. so if you have an object called "Customer" and you want to define a standard look and feel for "Customer"-you 'd use DataTemplate.

3..HierarchicalDataTemplate-Is a class that is a DataTemplate that is very well suited to hierarchical data.

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.