Sunday, November 1, 2009

Week 8 - Exam Make-up Points

For my blog entry this week, I will be writing about the difference between event-based and control-based triggers in order to earn a few points back on my exam grade.

Property-based triggers are those events that are triggered when certain properties change in an application. This, as I learned on the test, is NOT binding, but it has a similar idea to binding in that certain properties are tied to/bound to other properties whereas in binding, the contents of an object are bound to data in a list or something similar. With property-based triggers, when a certain property of one object changes, it affects/changes other properties.

Property-based triggers can be "embedded"/nested is various blocks of XAML. The following two examples use a ControlTemplate as well as a Style block.



In the above example, when the "HasDropShadow" is true, then the border of the same control has its CornerRadius property set to 4 and its SnapsToDevicePixels set to true.



In this second example, when the object has a mouse over it (IsMouseOver property), it is rotated 10 degrees and the foreground color becomes black. As you can see, these property-based triggers act like their name suggests: they are triggers that are based on changes in one property value that affects the value of other properties.

Event-based triggers are slightly different although they look a little bit similar on the XAML side. The biggest difference in event-based triggers is that they use C# code in addition to the XAML to enact the changes and can do a lot more than just adjust properties in XAML and the like.



public void OnCloseBtn(object o, RoutedEventArgs e)
{
Close();
}

In this example, you can see that the difference here is that, although the XAML still uses a "property" to trigger the event, the event is not necessarily property-based in that the properties of an object are those that define it. With event-based triggers, the Click "property" is used to trigger a change, but the Click property is basically an event hence the name event-based trigger.

In the example, when the button called closeButton is clicked, it calls the function OnCloseBtn which is located in the C# code (and displayed below the XAML in the above examle). When the OnCloseBtn is called, it closes the program. This is a very basic example, but the OnCloseBtn function could have much more complicated functionality and could use information sent via the obeject o and RoutedEventArgs e variables in the function definition.

As one can see, property-based and event-based triggers both get started in the XAML, but the biggest difference is that the event-based triggers are actually carried out in C# functions whereas property-based triggers are carried out in the XAML itself and usually are not as complex or powerful.

No comments:

Post a Comment