[Learning] WPF XAML Chapter II of properties and events

1, a simple attribute type converters and

  When adding the text box, the text box often alignment, font, font size and margin information, a text box as shown in the example:

<TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Margin="10,10,13,10" TextWrapping="Wrap" Grid.Row="0" FontFamily="Verdana" FontSize="24">
     [Please Question here!]
</TextBox>

  To enable the above functions is provided, System.Windows.Controls.TextBox class must provide the following properties: VerticalAlignment, HorizontalAlignment, FontFamily, FontSize, Foreground. To make these features work, XAML parser needs to be performed on more work than it looks. XML attribute values ​​are always plain text string. But the properties of the object can be any type of .NET. In the example above, there are two attributes enumerated type (the VerticalAlignment HorizontalAlignment properties and attributes), a string of type (the FontFamily property), of a plastic (the FontSize property), there is a Brush object (the Foreground property) .

  For non-string values ​​and associated string attribute, XAML parser needs to perform the conversion. Conversion performed by the converter type, a type of converter is a fundamental part of the infrastructure from .NET .NET 1.0 has been introduced.

  In fact, in this type of converter plays an important role in the process - provides practical methods that can be converted to a specific .NET data type to any other type .NET, or any other .NET type into a specific data types. XAML parser to find the type converter by the following two steps:

  (1) Check the property declaration, find TypeConverter characteristics (if provided TypeConverter feature that will develop a class which can perform conversion). For example, when the practical properties such as Foreground, .NET checks Foreground declaration attribute.

  (2) If there is no TypeConverter characteristic property declaration, XAML parser will check the type of data corresponding to the class declaration. For example, Foreground a Brush object attribute utility. Since Brush class uses TypeConverter (typeof (BrushConverter)) characteristic declared been modified, so its subclasses Brush type converter using BrushConverter.

  Type converter or if the property declaration class declarations are not associated, XAML parser generates an error.

2, complex property

  Although type converter easy to use, but can not solve all the practical problems. For example, some properties are complete objects that have their own set of attributes. Although created for a string type converter use is possible, but the use of this method syntax can be complex and error-prone.

  Fortunately, XAML alternatively provided: attribute syntax element (property-element syntax). Use attribute element method, may be added in the form of the name of the child element Parent.PropertyName. For example, the Grid Backgroud control has a property that allows a background region provided for drawing a drawing control brush. If you want to use a more complex brush - brush filling more advanced than a single solid color - you need to add a sub-label called Grid.Background of. As follows:

<Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop  Offset="0.00" Color="Red"/>
                    <GradientStop Offset="0.5" Color="Indigo"/>
                    <GradientStop Offset="1.0" Color="Violet"/>
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
</Grid.Background>

  In order to define the gradient need, we need to create LinearGraduebtBrush object. LinearGraduebtBrush class is part of the WPF namespaces in the collection, it is possible for the label to continue using the default XML namespace.

  However, it is not enough just to create LinearGraduebtBrush objects - also need to specify a color for the gradient. This work can be done by using the binding properties of the filling LinearGraduebtBrush.GradientStops GradientStop object.

3, markup extension

  For most property, XAML attribute syntax can work very well. But in some cases, you can not hard-coded property values. For example, it may be desirable to set the property value of an existing object, or may wish to control a property value set dynamically by a further property is bound to. Both cases require the use of markup extension - a setting property to unconventional methods specialized syntax.

  Tag may be used to expand or nested XML tag characteristics (for the case of the more common characteristics of XML). When used in characteristics, they are always surrounded by braces {}. For example, the following markup demonstrates how to use the extended, which allows a reference to another class of static properties:

<Button Foreground="{x:Static SystemColors.ActiveCaptionBrush}"></Button>

  Extended use of labeled marker extension class parameters {} syntax. In the above example, the marker is expanded StaticExtension class (by convention, when referring to the extended last word class may be omitted Extension). x prefix indicates Find StaticExtension class in XAML namespace. Some markup extension is part of the WPF namespaces, they do not require x prefix.

  All markup extensions by class inherits from the base class to achieve System.Windows.Markup.MarkupExtension. MarkupExtension base class is very simple - it provides a simple method of class ProvideValue obtain the desired value (). In other words, when the XAML parser encounters the above statement, it creates an instance of StaticExtension class, and then call ProvideValue () method to get a static object. Finally, find the Foreground property of the button.

4, additional properties

   In addition to the common attributes, XAML concept including additional attributes (attached property) - the additional attribute is an attribute defined in the plurality of control but a further class. In WPF, additional attributes common language control layout.

  Each control has its own inherent properties. When the mode is easy to control, depending on the type of vessel control gain additional feature (e.g., if a text box is placed in the grid, it is necessary to select a text box in the grid control unit which cell), an additional additional details of these property.

  Additional property always use the naming format consists of two parts: the definition of the type of property name. This two-part zho9ng naming syntax XAML interpreter can make distinguish common attributes and additional attributes.

In the following code, Grid.Row = "0" is to be additional properties.

<TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Margin="10,10,13,10" TextWrapping="Wrap" Grid.Row="0" FontFamily="Verdana" FontSize="24">
            [Please Question here!]
        </TextBox>
        
        <Button  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Grid.Row="1" Width="127" Height="23" Name="btnAnswer" Click="btnAnswer_Click">
            Ask the Eight Ball
        </Button>
        <TextBox Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" TextWrapping="Wrap" IsReadOnly="True" Margin="10,10,13,10" FontFamily="Verdana" FontSize="24" Foreground="Green">
            [Answer will appear here!]
        </TextBox>

  Additional properties are not really attribute. In fact they are converted into method calls. DefiningType.SetPropertyName (): XAML parser calling the static method takes the following form. For example, in the above XAML code segment defines the class type is a Grid, and the attribute is Row, so called by the parser Grid.SetRow () method.

  When calling setPropertyName () method, passing the parser two parameters: the object being modified, and the specified property values. For example, when the property is set Grid Textbox control, the XAML parser performs the following code:

Grid.SetRow(txtQuestion,0);

  In this way (a static method call defined types) hides the operation actually happened, very convenient to use.

  Such a technique has been able ridiculous, because, like all other WPF controls, TextBox controls inherit from DependencyObject base class.

2.5 Special characters and blank

  XAML is limited by the rules of XML. For example, XML particular attention to some special characters such as &, <and>. If you use these characters to set the elements of content, you will get into trouble, because XAML parser think you are dealing with something else - for example, create nested elements.

  For example, suppose you want to create a button <Click Me> containing the text, the following mark is not effective.

<Button>
    <Click Me>
</Button>

  The problem here is that the top mark looks like're trying to create a file called Click, and with the element <Click> text. Solution to the problem is to replace those with special character entity references, entity references are parsed XAML parser can correct a specific character encoding. The following table lists the possible choice of character entities. Doctrine, only when using a special set property values, character entities need to use quotes, because quotes are used to indicate the beginning and end of special means.

Special characters Character Entities
Less than (<) &lt;
Greater than (>) &gt;
&symbol(&) &amp;
quotation marks(") &quot;

  The above XAML code segment may be modified to:

<Button>
    &lt;Click Me &gt;
</Button>

  Special characters are not the only obstacle to the use of XAML. Another issue is blank treatment. By default, all the XAML folded blank, which means that spaces, Tab key, and a long string of hard return will be converted to a single space. Moreover, if a blank before or after adding the element content, will simply ignore this space. In Eight BallAnswer example, you will see this situation. In Text button and two text boxes, using a hard return characters separated out from the tag XAML, and Tab characters using the marking more legible. However, the extra space is no longer displayed in the user interface.

  Sometimes this is not all of the desired results. For example, you may want to include a series of spaces in the button text. In this case, the element requires xml: space = "preserve" characteristics.

  xml: space feature is part of the XML standard, is a either include all or nothing, including setting. By using this setting, all the elements within the empty string will be retained. Tag such as the following:

<TextBox xml:space="preserve">
    [There is a lot  of space inside these marks"        ".]
</TextBox>

2.6 Event

  The syntax used to associate the event handler as follows: event name = "event handler method name."

  For example, Button control provides the Click event. It may be used as shown below tag associated event handler:

<Button  Click="btnAnswer_Click">
            Ask the Eight Ball
</Button>

  The above method assumes marking btnAnswer_Click named in the code behind class. Event handler must have the correct signature (that is, must match the Click event delegate). Here is a satisfactory way:

private void btnAnswer_Click(object sender, RoutedEventArgs e)
        {

        }

  In many cases, set properties and associated event handlers for the same element will use characteristics. WPF always follow the following order: First, set the Name property (if the facility), and then associate any event handler, and finally set other properties. This means that all event handlers to respond to changes in the properties will be triggered when the first set properties.

  

  Full XAML code as follows:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Eight Ball Answer" Height="328" Width="412">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Margin="10,10,13,10" TextWrapping="Wrap" Grid.Row="0" FontFamily="Verdana" FontSize="24">
            [Please Question here!]
        </TextBox>
        <Button  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Grid.Row="1" Width="127" Height="23" Name="btnAnswer" Click="btnAnswer_Click">
            Ask the Eight Ball
        </Button>
        <TextBox Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" TextWrapping="Wrap" IsReadOnly="True" Margin="10,10,13,10" FontFamily="Verdana" FontSize="24" Foreground="Green">
            [Answer will appear here!]
        </TextBox>
        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop  Offset="0.00" Color="Red"/>
                    <GradientStop Offset="0.5" Color="Indigo"/>
                    <GradientStop Offset="1.0" Color="Violet"/>
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>
        

    </Grid>
</Window>

 

Guess you like

Origin www.cnblogs.com/Peter-Luo/p/12127755.html
Recommended