UserControl和CustomControl基础【PluraSight】

UserControl

UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后台程序的UserControl.Content属性所付的值。如下UserControl.Content包含一个Grid,里面有TextBox和Button.

所以可以run time改变usercontrol包含的内容。

 

1:什么时候用UserControl:

  • UserControl是多个已存在control组成的,为复用做准备(比如一个Context Form包含很多基础input,combobox textblock等,之后多次复用)
  • 不需要复杂的customzation的时候,因为其不支持theming和ControlTemplate,很难style。
  • Prefer code-behind model,warp一些eventhandler
  • Won't be shared across applicatio

2:举例说明:

见上面的xaml和c#截图

3:注意

UserControl里可以设置某个元素l的x:Name属性,这样后台可以直接访问该元素.(不像Custom Control UserControl里的任何x:Name都可以在后台程序直接调用,custom control要用Access Template Element

CustomerControl

1:什么时候使用:

  • Control doesn't exist
  • 增强现有control的功能
  • Need control  to support theming
  • Want shared across application

    

    如上结构,是自己重新add的一个custom control(程序会自动创建一个themes和其后台code),一般项目中如果是想re-write一个button的theme的话,不需要这么add,直接xaml中改其style    和controltemplate就好了。不管是那种情况都要表明TargetType.

    

2:CustomerControl关键点:

  • Choose Base Class:创建customercontrol的时候这点最重要,选择对的control可以使自己事半功倍。可以自己改写该control的Template,或者baseon已经提供了的resources
    • UIElement:most light weight base control,support layout, input, focus and event
    • FrameworkElement: 继承自UIElement,support style, tooltip, ContextMenus,同时它又是第一个应用logic tree的类,所以支持databinding, resource lookup
    • Control: Common base class for controls,support template, base properties(ForeGround, BackGround, FontSize)
    • ContentControl: Control have additional content property, hold single content
    • HeaderedContentControl: Like TabControl, GroupBox
    • ItemsControl: control have items collection, dynamic show data, but not support selection
    • Selector: It's one kind of ItemsControl,but the item can be indexed or selected(ListBox, ComboBox, TabControl)
    • RangeBase:Display range of value, add min max value property
    • Existing Control: Derive from existing control, like re-write button or ComboBox or Joey's labelMeasurementControl
  • Presenter是谁:

    这里主要是contentPresenter和itemPresenter的应用。

    ContentPresenter一般出现在<ControlTemplate>(用来改写ContentControl的Template属性)里面,其是用来显示ContentControl的Content属性。(可以是任何object:前台包含在该CustomControl tag里的内容或者是后台代码里Content的值)

    ItemPresenter 一般出现在<ControlTemplate>(用来改写ItemControl的Template属性) 资源里,用来显示ItemControl的Content属性(可以是任何object),注意ItemControl没有selection功能,不能知道当前选择了那个条目,需要选用继承自Selector类(如ListBox,ComboBox)。

  • 如何Data Binding:
  • Access Template Element:Customer Control即使在style中设置了某个元素的x:Name属性,后台不可以直接访问任何在Visual Tree里的元素,需要用到Access Template Element

3:举例说明:

<ResourceDictionary

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:CustomControlsIntro">





    <Style TargetType="{x:Type local:MyCustomControl}">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type local:MyCustomControl}">

                    <Border Background="{TemplateBinding Background}"

                            BorderBrush="{TemplateBinding BorderBrush}"

                            BorderThickness="{TemplateBinding BorderThickness}">

                        <TextBlock x:Name="textblock" Text="Custom COntrol" />

                    </Border>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>
View Code
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;



namespace CustomControlsIntro

{

    /// <summary>

    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.

    ///

    /// Step 1a) Using this custom control in a XAML file that exists in the current project.

    /// Add this XmlNamespace attribute to the root element of the markup file where it is 

    /// to be used:

    ///

    ///     xmlns:MyNamespace="clr-namespace:CustomControlsIntro"

    ///

    ///

    /// Step 1b) Using this custom control in a XAML file that exists in a different project.

    /// Add this XmlNamespace attribute to the root element of the markup file where it is 

    /// to be used:

    ///

    ///     xmlns:MyNamespace="clr-namespace:CustomControlsIntro;assembly=CustomControlsIntro"

    ///

    /// You will also need to add a project reference from the project where the XAML file lives

    /// to this project and Rebuild to avoid compilation errors:

    ///

    ///     Right click on the target project in the Solution Explorer and

    ///     "Add Reference"->"Projects"->[Browse to and select this project]

    ///

    ///

    /// Step 2)

    /// Go ahead and use your control in the XAML file.

    ///

    ///     <MyNamespace:MyCustomControl/>

    ///

    /// </summary>

    public class MyCustomControl : Control

    {

        static MyCustomControl()

        {

            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));

        }



        public MyCustomControl()

        {

            

        }

    }

}
View Code

使用两个UserControl和CustomControl:

使用UserControl和使用CustomeControl一样,xaml define UserControl的NS位置,然后就可以使用他们了。

 

 

 

你可能感兴趣的:(user)