代码下载
1.样式关键点
<UserControl.Resources>
<!-- 将样式应用于按钮button-->
<Style targettype="button" x:Key="t1">
<!-- 设置button上字体色为蓝色到红色渐变-->
<Setter Property="Foreground">
<Setter.Value>
<linearGradIEntBrush StartPoint="0,0.5" EndPoint="0.5,1">
<GradIEntStop color="Blue" Offset="0"/>
<GradIEntStop color="Red" Offset="1"/>
</linearGradIEntBrush>
</Setter.Value>
</Setter>
<!-- 设置button背景色为红色-->
<Setter Property="Background">
<Setter.Value >
red
</Setter.Value>
</Setter>
<!-- 设置button字体字号为22-->
<Setter Property="FontSize" Value="22"></Setter>
</Style>
<!-- 将样式应用于TextBox-->
<Style targettype="TextBox" x:Key="txt1">
<!-- 设置TextBox背景色为蓝色-->
<Setter Property="Background" Value="blue"></Setter>
</Style>
<!-- 将样式继承关系-->
<Style targettype="TextBox" x:Key="txt2" BasedOn="{StaticResource txt1}">
<!-- 设置TextBox背景色为蓝色-->
<Setter Property="FontSize" Value="12"></Setter>
<!-- 覆盖父样式-->
<Setter Property="Background" Value="Red"></Setter>
</Style>
<linearGradIEntBrush x:Key="l1">
<GradIEntStop color="Blue" Offset="0"></GradIEntStop>
<GradIEntStop color="Black" Offset="1"></GradIEntStop>
</linearGradIEntBrush>
</UserControl.Resources>
2.应用样式代码
<GrID x:name="LayoutRoot" Background="White">
<GrID.RowDeFinitions >
<RowDeFinition></RowDeFinition>
<RowDeFinition></RowDeFinition>
<RowDeFinition></RowDeFinition>
</GrID.RowDeFinitions>
<GrID.ColumnDeFinitions>
<ColumnDeFinition></ColumnDeFinition>
<ColumnDeFinition></ColumnDeFinition>
<ColumnDeFinition></ColumnDeFinition>
</GrID.ColumnDeFinitions>
<!-- TextBox应用样式-->
<TextBox Style="{StaticResource txt1}" Height="30" WIDth="100" GrID.Row="0" GrID.Column="0"></TextBox>
<!-- TextBox不应用样式-->
<TextBox Height="30" WIDth="100" GrID.Row="1" GrID.Column="0"></TextBox>
<!-- TextBox应用继承的样式-->
<TextBox Height="30" WIDth="100" GrID.Row="2" GrID.Column="0" Text="TextBox应用继承的样式" Style="{StaticResource txt2}"></TextBox>
<!-- button不应用样式-->
<button x:name="b2" Height="30" WIDth="100" GrID.Row="0" GrID.Column="1" Content="bt1"></button>
<button x:name="b3" Height="30" WIDth="100" GrID.Row="1" GrID.Column="1" Content="bt1"></button>
<button x:name="b4" Height="30" WIDth="100" GrID.Row="2" GrID.Column="1" Content="中华人民共和国" Foreground="{StaticResource l1}"></button>
<!-- button应用样式-->
<button x:name="b1" GrID.Row="2" GrID.Column="2" Content="得到资源值" Style="{StaticResource t1}" Click="b1_Click" margin="16,35,23"></button>
</GrID>
3.说明
1)可以使用X:key。也可以使用X:name来定义资源。
不同的是,使用X:name必须是唯一的,使用X:key在一个范围内必须唯一,但是在小范围之外是可以重复。
关键在cs代码方法如果是X:name,可以直接把name当作cs的对象来使用,但是如果是key,必须使用this.Resources["l1"];来获得属性
2)样式Style基本是为特定的元素类型分配的属性集合,除了有Key外,还有一个targettype属性,设置作用的控件类型。
3)样式的继承使用BaSEOn={StaticResource btnStyle}
继承父样式以后,可以直接使用父样式中的定义,如果有重复的定义,子样式会覆盖父样式的内容
3.执行效果
补充文档
新增样式代码
<UserControl xmlns:my="clr-namespace:SLUI" x:Class="SLUI.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="600" d:DesignWIDth="800" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <GrID x:name="LayoutRoot" Style="{StaticResource GrIDStyle}"> <GrID.ColumnDeFinitions > <ColumnDeFinition ></ColumnDeFinition> </GrID.ColumnDeFinitions> <GrID.RowDeFinitions > <RowDeFinition Height="auto" MinHeight="600"></RowDeFinition> </GrID.RowDeFinitions> <border name="borderAll" CornerRadius="10" Height="500" Style="{StaticResource borderStyle}"> <StackPanel Height="500" HorizontalAlignment="Center" name="stackPanel3" VerticalAlignment="top" Style="{StaticResource StackPanelStyle}" OrIEntation="Vertical" FlowDirection="RightToleft" > <!--大标题高50--> <StackPanel name="stackPanelBigTitle" Style="{StaticResource StackPanelBigTitleStyle}"> <TextBlock name="txtBigTitle" Text="借款申请单" Style="{StaticResource TextBlockBigTitleStyle}"/> </StackPanel> <!--小标题高30--> <StackPanel name="stackPanel1" Style="{StaticResource StackPanelSmallTitleStyle}"> <TextBlock name="txtTitle" Text="基本信息" Style="{StaticResource TextBlockSmallTitleStyle}"/> </StackPanel> <!--工具栏30--> <StackPanel name="stackPanel2" Style="{StaticResource StackPanelToolStyle}" > <button Content="取消" name="button2" WIDth="75" Style="{StaticResource buttonStyle}"/> <button Content="保存" name="btnSave" WIDth="75" Style="{StaticResource buttonStyle}"/> </StackPanel> <!--内容100--> <border Height="100" name="border1" Style="{StaticResource borderContentStyle}" > <StackPanel Height="99" name="stackPanelContent1" Style="{StaticResource StackPanelContentStyle}"> <GrID Height="102" FlowDirection="leftToRight"> <GrID.ColumnDeFinitions> <ColumnDeFinition/> <ColumnDeFinition/> <ColumnDeFinition/> <ColumnDeFinition/> </GrID.ColumnDeFinitions> <GrID.RowDeFinitions> <RowDeFinition/> <RowDeFinition/> <RowDeFinition/> <RowDeFinition/> </GrID.RowDeFinitions> <sdk:Label margin="27,6,51,3" d:LayoutOverrIDes="Height" Content="申请单编号"/> <sdk:Label GrID.Column="2" margin="28,10,3" d:LayoutOverrIDes="Height" Content="申请单名称"/> <sdk:Label margin="27,5,4" GrID.Row="1" d:LayoutOverrIDes="Height" Content="项目编号"/> <sdk:Label GrID.Column="2" margin="28,26,4" GrID.Row="1" d:LayoutOverrIDes="Height" Content="项目名称"/> <sdk:Label margin="27,4,43,5" GrID.Row="2" d:LayoutOverrIDes="Height" Content="业务类别"/> <sdk:Label GrID.Column="2" margin="28,42,0" GrID.Row="2" Content="币种"/> <sdk:Label margin="27,3,59,6" GrID.Row="3" d:LayoutOverrIDes="Height" Content="借款单位"/> <sdk:Label GrID.Column="2" margin="28,6" GrID.Row="3" d:LayoutOverrIDes="Height" Content="是否合作项目"/> <ComboBox GrID.Column="1" margin="5,2,1,3" GrID.Row="2"/> <ComboBox GrID.Column="3" margin="-14,20,2" GrID.Row="2"/> <CheckBox Content="" GrID.Column="3" margin="-13,-2" GrID.Row="3"/> <ComboBox GrID.Column="1" margin="5,4" GrID.Row="3" Height="21" VerticalAlignment="Bottom"/> <TextBox GrID.Column="1" margin="5,2" textwrapPing="Wrap" Text=""/> <TextBox GrID.Column="3" margin="-14,1" textwrapPing="Wrap" Text=""/> <TextBox GrID.Column="1" margin="5,1" GrID.Row="1" textwrapPing="Wrap" Text="" d:LayoutOverrIDes="Height"/> <TextBox GrID.Column="3" margin="-14,1" GrID.Row="1" textwrapPing="Wrap" Text=""/> </GrID> </StackPanel> </border> <!--小标题高30--> <StackPanel name="stackPanelTitle2" Style="{StaticResource StackPanelSmallTitleStyle}"> <TextBlock name="txtTitle2" Text="合作方信息" Style="{StaticResource TextBlockSmallTitleStyle}"/> </StackPanel> <!--工具栏30--> <StackPanel name="stackPanelTool2" Style="{StaticResource StackPanelToolStyle}"> <button Content="添加合作方信息" name="btnSave2" WIDth="100" Style="{StaticResource buttonStyle}"/> </StackPanel> <!--内容100--> <border Height="100" name="border2" Style="{StaticResource borderContentStyle}"> <StackPanel Height="100" name="stackPanelContent2" Style="{StaticResource StackPanelContentStyle}"> <sdk:DataGrID Height="100" Background="White" FlowDirection="leftToRight"> <sdk:DataGrID.Columns> <sdk:DataGrIDCheckBoxColumn header="选择"/> <sdk:DataGrIDTextColumn header="合作方名称"/> <sdk:DataGrIDTextColumn header="出资比例"/> <sdk:DataGrIDTextColumn header="出资金额"/> </sdk:DataGrID.Columns> </sdk:DataGrID></StackPanel> </border> <!--小标题高30--> <StackPanel name="stackPanelTitle3" Style="{StaticResource StackPanelSmallTitleStyle}"> <TextBlock name="txtTitle3" Text="支付信息" Style="{StaticResource TextBlockSmallTitleStyle}"/> </StackPanel> <!--工具栏30--> <!--内容100--> <border Height="100" name="border3" Style="{StaticResource borderContentStyle}"> <StackPanel Height="100" name="stackPanelContent3" Style="{StaticResource StackPanelContentStyle}"></StackPanel> </border> </StackPanel> </border> </GrID></UserControl>
效果:
代码下载
总结以上是内存溢出为你收集整理的Silverlight之资源样式的应用(5)全部内容,希望文章能够帮你解决Silverlight之资源样式的应用(5)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)