sl基础(二)

理解Styles和ControlTemplates

理解ResourceDictionaries

理解用户自定义控件

ControlTemplate从Control继承 而Control中有Template属性

定义ControlTemplate有三种方式:

内联定义:

<ToolTip>

    <ToolTip.Template>

        <ControlTemplate TargetType=”ToolTip>

             ………

        </ControlTemplate>

    </ToolTip.Template>

</ToolTip>

资源引用:

<UserControl.Resources>

<ControlTemplate x:Key=”tooltip” TargetType=”ToolTip”>

…….

</ControlTemplate>

</UserControl.Resources>

 

<Grid x:Name=”LayoutRoot” Background=”White”>

<ToolTip>

<ToolTip.Template>

          <StaticResources ResourceKey=”toolTip”/>

</ToolTip.Template>

</ToolTip>

</Grid>

样式引用 :(常用方式)

<UserControl.Resources>

<ControlTemplate x:Key=”tooltip” TargetType=”ToolTip”>

          <Setter Property=”Template”>

                <Setter.Value>

                        <ControlTemplate TargetType=”ToolTip”>

                         </ControlTemplate>

                </Setter.Value>

          </Setter>

</ControlTemplate>

</UserControl.Resources>

 

<ToolTip Style=”{StaticResource toolTip}”/>

DataPicker的水印处理

public class MyDatePicker : DatePicker
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var datePickerTextBox = GetTemplateChild("TextBox") as DatePickerTextBox;
            datePickerTextBox.Watermark = "选择日期....";
            datePickerTextBox.TextChanged += datePickerTextBox_TextChanged;
        }
        private void datePickerTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var datePickerTextBox = GetTemplateChild("TextBox") as DatePickerTextBox;
            datePickerTextBox.Watermark = "选择日期...";
        }
    }

FallbackValue属性:

<TextBlock Text=”{Binding Path=Name, FallbackValue=’无效值’}”/>

当绑定源不存在Name属性时,TextBlock就会显示无效值

<TextBlock Text=”{Binding Path=Data,StringFormat=’Now is \{0:yyyy-M-dd}’}”/>

园子里面看到统一设置样式的文章,感觉没有CSS来的方便,可能是刚入门还没有找到有效方法。<Application.Resources>
    <!--   统一设置按钮样式 TargetType应用的类型 x:Key 名称  -->
    <!--   引用方式: Style=" { StaticResource ButtonStyle }"  -->
    <Style TargetType="Button" x:Key="ButtonStyle">
        <Setter Property="Background" Value="Red"></Setter>
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Margin" Value="10"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontSize" Value="16"/>
    </Style>
</Application.Resources>

你可能感兴趣的:(基础)