WPF-控件-编辑圆角TextBox
2014-02-25 15:35
429 查看
使用模板
代码如下:
效果如下:
另一种方式
因为Style没有x:Key标记,默认为应用到所有由x:Type指定的控件上,如果不想应用则需要把空间的Style标记为{x:Null}就行。
代码如下:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="模板.MainWindow" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"> <GradientStop Color="#ABADB3" Offset="0.05"/> <GradientStop Color="#E2E3EA" Offset="0.07"/> <GradientStop Color="#E3E9EF" Offset="1"/> </LinearGradientBrush> <Style x:Key="RoutedCornerTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Padding" Value="1"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <!--<Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Themes:ListBoxChrome> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers>--> <Border x:Name="Bd" SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="15" > <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"></ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBox x:Name="textBox1" BorderBrush="Red" Margin="10" Height="40" Grid.Row="0" Style="{DynamicResource RoutedCornerTextBoxStyle}"/> <TextBox x:Name="textBox2" BorderBrush="Green" Margin="10" Height="40" Grid.Row="1"/> </Grid> </Window>
效果如下:
另一种方式
<Window x:Class="圆角TextBox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type TextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border x:Name="Bd" SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5"> <ScrollViewer x:Name="da" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"></ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="Margin" Value="5"/> <Setter Property="BorderBrush" Value="Green"/> <Setter Property="Height" Value="25"/> <Setter Property="Width" Value="200"/> </Style> </Window.Resources> <StackPanel> <TextBox/> <TextBox/> <TextBox Style="{x:Null}" Height="20" Width="100" Margin="10"></TextBox> </StackPanel> </Window>
因为Style没有x:Key标记,默认为应用到所有由x:Type指定的控件上,如果不想应用则需要把空间的Style标记为{x:Null}就行。
相关文章推荐
- C# 7个读写Excel文件的类库
- openssl 移植到 arm
- Longest Substring Without Repeating Characters - LeetCode
- qt中qDebug()无法输出解决办法
- 通信十年,通信真是个死胡同吗?
- SIFT算法详解
- Ruby自学笔记(二)— Ruby的一些基础知识
- (原)android修改文件所属的用户组
- 用户全选管理设计篇
- ubuntu设置系统时间与网络时间同步
- 验证控件jquery.validate.js使用说明+中文API
- 定时更新mysql的表数据
- display和visibility的区别
- 安卓搭建环境常见的术语
- mgo map 处理源码阅读
- 定位android kernel crash的方式
- MySQL锁的用法之行级锁
- 设置Ubuntu上的MySQL可以远程访问
- 软件需求分析(理论)
- 在android的spinner中,实现取VALUE值和TEXT值