首页 新闻 会员 周边 捐助

WPF中怎么实现鼠标点上去后修改Textbox的样式

0
悬赏园豆:10 [已解决问题] 解决于 2010-08-23 13:12

我想实现VS中属性窗口的功能,鼠标没有悬上去的时候是label,悬上去是textbox,当选中的时候可以输入,label的时候跟背景同色,选中时候背景发生变化

蝶殇的主页 蝶殇 | 初学一级 | 园豆:32
提问于:2010-08-18 13:46
< >
分享
最佳答案
1

我比较习惯用样式去处理这些问题

添加一个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>
收获园豆:5
peony007 | 菜鸟二级 |园豆:397 | 2010-08-20 15:56
其他回答(2)
0

添加各种控件事件来动态生成控件或绑定一些操作。

Astar | 园豆:40805 (高人七级) | 2010-08-18 14:23
0

在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;
        }
收获园豆:5
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-18 14:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册