|
Today we wrap up dependency properties by talking about one of the most important, though typically invisible aspects: DP precedence order. This is critical because dependency properties, by their nature, are designed to have their final value set from more than one influence (e.g., resources, data binding, animation, etc.) The rule is rigid, sensible and straight forward… Animation uber alles. If there is an animation affecting the value, that will take precedence over all other values Local values over everything but Animation In the absence of an animation, use the local value to override any other values Templates/Styles if no animation or local values If there is no animation or local value, use the value from the template, and if there is no template then from the style. When all else fails, use the default If none of the above apply, use the default value for that type. The Default Value It is the last rule that demands that every type have a default look, and it is because of that rule that you crated generic.xaml – so that if your custom control has a value that is not controlled by an animation, a local value, a template or a style, the Dependency Property System will turn to the default values in generic.xaml and use those. Our First Application With this, we're ready to examine the code to our first application. As you may remember , we have three projects in one solution. In CustomControl.cs we declare the visual logic of our control and we will, eventually declare the logic (event handlers, etc.) as well. For now, the listing is quite simple: 1: using System.Windows.Controls; 2: 3: namespace ClassLibrary 4: { 5: public class CustomControl : Control 6: { 7: public CustomControl() 8: { 9: DefaultStyleKey = typeof ( CustomControl ); 10: } 11: } 12: } The simplest logic you can add to get the default style to show. The default style, as you remember is in the file generic.xaml which in turn is in the Themes folder, 1: < ResourceDictionary 2: xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3: xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 4: xmlns:controls ="clr-namespace:ClassLibrary" 5: xmlns:vsm ="clr-namespace:System.Windows;assembly=System.Windows" 6: xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" 7: xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" 8: mc:Ignorable ="d" > 9: < Style...
|