WPF Miscellaneous -- custom controls and wpf miscellaneous controls

Source: Internet
Author: User

WPF Miscellaneous -- custom controls and wpf miscellaneous controls

If you only use the existing WPF control, it is difficult to meet the complex business of the current society. Therefore, it is inevitable for users to customize a series of controls. WPF has two types of control: User Control and custom control. I believe that I can understand the difference between them after reading the previous chapter. It is not difficult to understand user controls-controls formed by combining existing controls. In my opinion, custom controls are the most attractive part of WPF.

A user control is often a xaml file or a DLL file. However, custom controls are usually a DLL file. When referencing this DLL file, the application will load the Generic. xaml file in the Theme folder in the dll file to publicize the custom control. The Generic. xaml file contains several custom control xaml files. As follows.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:FirstFloor.ModernUI.Windows.Controls"
                    xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/FirstFloor.ModernUI;component/Themes/BBCodeBlock.xaml" />
        <ResourceDictionary Source="/FirstFloor.ModernUI;component/Themes/ModernButton.xaml" />
        <ResourceDictionary Source="/FirstFloor.ModernUI;component/Themes/ModernDialog.xaml" />
    </ResourceDictionary.MergedDictionaries>
 
</ResourceDictionary>

Is there anything similar to App. xaml. When we reference this custom control DLL file, it loads the current Theme Folder/Generic. xaml file. Note that you must specify the relevant Style type in the constructor of the custom control class. As follows.

   public ModernButton()
        {
            this.DefaultStyleKey = typeof(ModernButton);
        }

Suppose we have compiled several custom controls. To facilitate reference of these controls. We often use the path-based function. As follows:

  xmlns:mui="http://firstfloorsoftware.com/ModernUI"

Set the reference path. The page can be used as follows.

  <mui:ModernButton Content="modern button" IconData="{StaticResource HomeIconData}" Margin="0,0,0,8" />

If you do not use the reference path, you must reference the relevant namespace when using the custom control. Similar to the following

 xmlns:app="clr-namespace:FirstFloor.ModernUI.App"

What if the control has different namespaces? Sorry, please introduce all the relevant namespaces. But the referenced path is not required. Why? Let's take a look.

[assembly: XmlnsDefinition("http://firstfloorsoftware.com/ModernUI", "FirstFloor.ModernUI.Presentation")]
[assembly: XmlnsDefinition("http://firstfloorsoftware.com/ModernUI", "FirstFloor.ModernUI.Windows")]
[assembly: XmlnsDefinition("http://firstfloorsoftware.com/ModernUI", "FirstFloor.ModernUI.Windows.Controls")]
[assembly: XmlnsDefinition("http://firstfloorsoftware.com/ModernUI", "FirstFloor.ModernUI.Windows.Converters")]
[assembly: XmlnsDefinition("http://firstfloorsoftware.com/ModernUI", "FirstFloor.ModernUI.Windows.Navigation")]
[assembly: XmlnsPrefix("http://firstfloorsoftware.com/ModernUI", "mui")]

This part of the code is in the AssemblyInfo. cs file of the FirstFloor. ModernUI project of the Modern UI for WPF open-source project. The FirstFloor. ModernUI Project is a custom control project. The reference path above can be said to be a one-to-many relationship. I don't need to explain it.

Remember that every user control in the previous chapter will be implemented in the FrameworkElement class. In the FrameworkElement class, there is an attribute called Template. It is the core point of the custom control. Literally, it means the template. This is what I understand. The custom control is actually a display template of the control, which is painted with its own meaning. When creating a custom control, the author VS helped to generate the following code. I will make some modifications. The author adds a TextBox. The Text is AomiCustomTextBox.

<Style TargetType="{x:Type local:CustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBox Text="AomiCustomTextBox"></TextBox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
 </Style>

Display result:


If Template (ControlTemplate) is used for custom controls. What is a DataTemplate similar to him used? In fact, we often use them when developing WPF. If you do not know the differences between them, they may be mistaken. As defined above on MSDN. ControlTemplate is used to determine the visual structure and behavior of a control. Note that a widget is used. DataTemplate is a visualized structure that determines a business object. That is, the object orientation is different. It is the fact that the development process sometimes feels like this. ControlTemplate generally uses TemplateBinding to bond data, while the DataTemplate uses the standard binding. What does it mean? That is to say, ControlTemplate generally looks for the dependency attribute of the control. Instead, DataTemplate is used to find DataContext.

Can they be used at the same time? The answer is yes. Be careful at this time. There are often some mutually influential things. However, it must be clear that ControlTemplate is a display regardless of the number of services. It only controls the layout structure. DataTemplate and vice versa.

Reference definition controls cannot be separated from the Generic. xaml file in the Theme folder. So here I have a question: What is the difference between the reference in App. xaml and the reference in the Generic. xaml file? All controls of WPF have a recognizable style. This style is usually determined by the DefaultStyleKey dependency attribute of the control. The default style file names are often different based on the current operating system. As follows:

1.Generic. xaml: It will be referenced if none of the following types are recognized. I like standard styles.

2.Luna. Xxx. xaml: the recognition style referenced in the XP system. For example:Luna.Homestead.xaml、Luna.Metallic.xaml

3.Aero. Xxx. xaml: InThe token recognition style referenced in Vista. For example:Aero.NormalColor.xaml

Therefore, it is difficult for me to distinguish the differences between App. xaml and Generic. xaml. Because the two are very similar. I made a slight experiment to see who is executing App. xaml and Generic. xaml first. The result is Generic. xaml. Therefore, if you define a style in App. xaml, an error will be reported if you want to reference StaticResource in Generic. xaml. Specifically, I think you can give it a try. It is worth noting that the resource files referenced by App. xaml are usually styles of standard controls (colors, controls of WPF. Generic. xaml is generally used to customize control resource files.


Related Article

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.