Wpf binding template




















Basically, what you put into a DataTemplate determines what the data looks like in the rendered app. In our sample app, each custom Photo object has a Source property of type string that specifies the file path of the image.

Currently, the photo objects appear as file paths. For the photos to appear as images, you create a DataTemplate as a resource. If your DataTemplate is in the resources section, when you specify the DataType property to a type and omit an x:Key , the DataTemplate is applied whenever that type appears.

You always have the option to assign the DataTemplate with an x:Key and then set it as a StaticResource for properties that take DataTemplate types, such as the ItemTemplate property or the ContentTemplate property. Essentially, the DataTemplate in the above example defines that whenever there is a Photo object, it should appear as an Image within a Border. With this DataTemplate , our app now looks like this.

The data templating model provides other features. For example, if you are displaying collection data that contains other collections using a HeaderedItemsControl type such as a Menu or a TreeView , there is the HierarchicalDataTemplate.

Another data templating feature is the DataTemplateSelector , which allows you to choose a DataTemplate to use based on custom logic. For more information, see Data Templating Overview , which provides a more in-depth discussion of the different data templating features.

A trigger sets properties or starts actions, such as an animation, when a property value changes or when an event is raised.

Style , ControlTemplate , and DataTemplate all have a Triggers property that can contain a set of triggers. There are several types of triggers. A Trigger that sets property values or starts actions based on the value of a property is called a property trigger.

To demonstrate how to use property triggers, you can make each ListBoxItem partially transparent unless it is selected. The following style sets the Opacity value of a ListBoxItem to 0. When the IsSelected property is true , however, the Opacity is set to 1. This example uses a Trigger to set a property value, but note that the Trigger class also has the EnterActions and ExitActions properties that enable a trigger to perform actions.

In the following illustration, the third item is the selected item. Another type of trigger is the EventTrigger , which starts a set of actions based on the occurrence of an event. For example, the following EventTrigger objects specify that when the mouse pointer enters the ListBoxItem , the MaxHeight property animates to a value of 90 over a 0.

When the mouse moves away from the item, the property returns to the original value over a period of 1 second. Note how it is not necessary to specify a To value for the MouseLeave animation. This is because the animation is able to keep track of the original value.

For more information, see the Storyboards overview. In addition to Trigger and EventTrigger , there are other types of triggers. MultiTrigger allows you to set property values based on multiple conditions. Controls are always in a specific state. A Data Template defines and specifies the appearance and structure of a collection of data. It provides the flexibility to format and define the presentation of the data on any UI element.

There is a button and a list box as well which to display the data. Here is implementation in C in which a list of Person objects are assigned to DataContext, implementation of Person class and button click event. When you compile and execute the above code, it will produce the following window. It contains one list and inside the list box, each list box item contains the Person class object data which are displayed on Labels and Text boxes.

At this point of time, it works OK, but there are scenarios where this struct breaks down. For example, let's increase the height of button , from 35 to as:. In the above image, you will notice that button height is increased but the ellipse size is still the same, which is a bad UI design.

And the reason this is happening is, inside a ControlTemplate , the height of an ellipse is hard coded. So, no matter, whatever height is set at parent i. So, now we have to fix this problem by using a special type of binding called TemplateBinding inside ControlTemplate. So, instead of hard coding the height, we will use TemplateBinding as shown below:. By setting the TargetType property of ControlTemplate , we are telling Ellipse that, any property of Button can be set to ellipse. However, it still looks a lot like a ListBox.

A very common usage scenario for a ListView is to have columns, sometimes e. WPF comes with a built-in view class to handle this, which we will talk about in the next chapter. WPF Tutorial. Previous Next.



0コメント

  • 1000 / 1000