wpf textbox 加按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf textbox 加按钮相关的知识,希望对你有一定的参考价值。
在WPF中,如何为textbox添加按钮,是在里面添加按钮的,比如清除按钮,软件键盘按钮,这个如何实现
推荐你两种方法:
创建自定义控件,改自定义控件由TextBox和Button组合而成。(这个就不给代码了)
修改TextBox的模板或样式。代码如下:
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="WpfApplication3.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="TextBoxStyle1" 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="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<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">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels"/>
<Button Grid.Column="1" Background="Blue" />
</Grid>
</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>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TextBox HorizontalAlignment="Left" Height="38" Margin="150,105,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="168" Style="DynamicResource TextBoxStyle1"/>
</Grid>
</Window>
其中
<Themes:ListBoxChrome x:Name="Bd" BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Background="TemplateBinding Background" RenderMouseOver="TemplateBinding IsMouseOver" RenderFocused="TemplateBinding IsKeyboardFocusWithin" SnapsToDevicePixels="true"><Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels"/>
<Button Grid.Column="1" Background="Blue" />
</Grid>
</Themes:ListBoxChrome>
原来为:
<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>
如有疑问,继续追问。
参考技术A 重写控件模板、使用自定义控件、写成用户控件都是可以的 参考技术B 之定义控件,自己画一个以上是关于wpf textbox 加按钮的主要内容,如果未能解决你的问题,请参考以下文章