我比较习惯用样式去处理这些问题
添加一个TextBox
首先在鼠标没有任何动作的时候,希望如Label一样显示,我想将TextBox背景和边框置为无就可以了,至于鼠标操作的效果都去样式里设定。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="windowdrer" x:Class="WpfApplication1.Window1"
Title="Window1" mc:Ignorable="d" WindowStartupLocation="CenterScreen" d:DesignWidth="228" d:DesignHeight="243" Width="220" Height="240" >
<Window.Resources>
<SolidColorBrush x:Key="ListBorder" Color="#FF7F9DB9"/>
<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<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>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd" Value="#FF81D8FF"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" TargetName="Bd" Value="Red"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="#FF00EE51"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Text="TextBox" TextWrapping="Wrap" Background="{x:Null}" BorderBrush="{x:Null}" Style="{DynamicResource TextBoxStyle1}"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Text="TextBox" TextWrapping="Wrap" Margin="0,40,0,0"/>
</Grid>
</Window>
添加各种控件事件来动态生成控件或绑定一些操作。
在TextBox 的 GotFocus 与 LostFocus 这两个事件结合
代码:
XAML:
<TextBox x:Name="txt" GotFocus="txt_GotFocus" LostFocus="txt_LostFocus"></TextBox>
CS:
private void txt_GotFocus(object sender, RoutedEventArgs e)
{
this.txt.Background = Brushes.Red;
}
private void txt_LostFocus(object sender, RoutedEventArgs e)
{
this.txt.Background = Brushes.Blue;
}