[WPF Series]-Data bonding datatemplate using DataTrigger to apply attribute values

Source: Internet
Author: User

Using DataTrigger to apply attribute values

The current representation does not tell us whether a task is a family task or an office task. Remember that the Task object has a TaskType property of type TaskType, which is an enumeration whose values can be Home and work.

In the following example, DataTrigger sets the borderbrush of the border element to Yellow if the TaskType property is Tasktype.home.

<DataTemplatex:key= "Mytasktemplate">...<datatemplate.triggers>  <DataTriggerBinding="{Binding Path=tasktype}">    <Datatrigger.value>      <Local:tasktype>Home</Local:tasktype>    </Datatrigger.value>    <SetterTargetName= "Border" Property= "BorderBrush"Value= "Yellow"/>  </DataTrigger></datatemplate.triggers>...</DataTemplate>

The border of the home task is displayed in yellow, and the boundaries of the Office task appear as light green.

In this example, DataTrigger uses a Setter to set the property value. The trigger class also has the Enteractions and Exitactions properties, which allow you to start a set of actions, such as animation operations. Also, there is a Multidatatrigger class that allows you to apply changes based on multiple data-bound property values.

Using converters to create these effects is a bit more efficient in terms of performance. In addition, creating your own converters gives you more flexibility because you provide your own logic. Finally, the technology you choose depends on your specific situation and your preferences. For information about how to write converters, see IValueConverter.

What's in DataTemplate?

In the previous example, we used the Datatemplate.triggers property to put the trigger into DataTemplate. The Setter of the trigger sets the property value of the element (Border element) in DataTemplate. however , if your Setters -related property is not a property of the element in the current DataTemplate, it is more appropriate to set the property using the Style (for the ListBoxItem Class) (if the control you are binding to is a ListBox). For example, if you want your Trigger to animate an item's Opacity value when the mouse points to an item, you need to define the trigger in the ListBoxItem style. For an example, see Introduction to Styling and templating sample (example of styling and templating).


It is often important to note that DataTemplate is applied to each generated listboxitem (for more information about how and where it is actually applied, see ItemTemplate page). DataTemplate is only relevant to the representation and appearance of the data object. In most cases, all other presentation aspects (for example, the appearance of items when they are selected or how items are arranged in a ListBox) are not within the scope of the DataTemplate definition. For an example, see the section on styling and template handling for ItemsControl.

Reference

[WPF Series]-Data bonding datatemplate using DataTrigger to apply attribute values

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.