wpf textbox 加按钮

wpf textbox 加按钮,第1张

推荐你两种方法:

创建自定义控件,改自定义控件由TextBox和Button组合而成。(这个就不给代码了)

修改TextBox的模板或样式。代码如下:

<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.Themesassembly=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>

如有疑问,继续追问。

试了下,构造函数里先加上这句就可以了:

this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11670776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存