当文本框焦点时改变容器的背景颜色 [英] changing background color of container when textbox is in focus
问题描述
我有一个简单的用户控件,使用 TextBox
。我想在 TextBox
获得焦点时更改用户控件的颜色。这是我有:
< UserControl x:Class =OutLookContactList.ContactSearchControl
xmlns =http ://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
x:Name = rootMinHeight =30Loaded =UserControl_Loaded>
< UserControl.Resources>
< Style x:Key =searchTextBoxStyleTargetType ={x:Type TextBox}>
< Style.Triggers>
<触发属性=IsFocused值=true>
< Setter TargetName =rootProperty =BackgroundValue ={StaticResource OnMouseOverColor}/>
< / Trigger>
< / style>
< /UserControl.Resources>
但是我得到了errotTargetName属性不能在样式Setter上设置。当文本框获得焦点时,如何设置用户控件的背景颜色?
感谢你们
它是否可以用来包装 UserControl
在
边界
对象内?如果是这样的话,你可以简单地设置 Border >样式:
< ; UserControl x:Class =Sample2.ContactSearchControl
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http:// schemas .microsoft.com / winfx / 2006 / xaml
Height =75Width =300>
< Border>
< Border.Style>
< Style TargetType =边框>
< Setter Property =BackgroundValue =White/>
< Style.Triggers>
< DataTrigger Binding ={Binding IsFocused,ElementName = txtSearch}Value =true>
< Setter Property =BackgroundValue =Black/>
< / DataTrigger>
< / style>
< /Border.Style>
< StackPanel>
< TextBox x:Name =txtSearchText =Search/>
< TextBox Text =其他/>
< / StackPanel>
< / Border>
< / UserControl>
更新:(回答Sheraz的问题)
我不确定为什么 ElementName
不能用于访问 UserControl中的子项
。这可能与构建可视化树的方式有关。
至于 据我所知, I have a simple user control with a But I get the errot "TargetName property cannot be set on a style Setter". How can I Set the back ground color of user control when text box gets the focus?
Thanks a bunch Will it work to wrap the contents of your Update: (Answering Sheraz' Questions) I'm not sure why As for As I understand it, the 这篇关于当文本框焦点时改变容器的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!触发器
vs DataTrigger
:触发器 a>用于依赖属性,而 DataTrigger 用于数据绑定属性(data 或其他控件)。既然你正在试图设置 Border 的样式,把
比 DataTrigger
放在那里更有意义, code> TextBox TextBox
更改 Border的外观 。
Targetter
属性 Setter
仅适用于 DataTemplate
或 ControlTemplate
内。 (来自Dr. WPF的信息这个论坛帖子)TextBox
. I want to change the color of user control when the TextBox
gets the focus. This is what I have:<UserControl x:Class="OutLookContactList.ContactSearchControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="root" MinHeight="30" Loaded="UserControl_Loaded">
<UserControl.Resources>
<Style x:Key="searchTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter TargetName="root" Property="Background" Value="{StaticResource OnMouseOverColor}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
UserControl
inside a Border
object? If so, you can simply style the Border
like so:<UserControl x:Class="Sample2.ContactSearchControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="75" Width="300">
<Border>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtSearch}" Value="true">
<Setter Property="Background" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<StackPanel>
<TextBox x:Name="txtSearch" Text="Search" />
<TextBox Text="Other" />
</StackPanel>
</Border>
</UserControl>
ElementName
doesn't work for accessing children within a UserControl
. It might have something to do with the way the visual tree is constructed.Trigger
vs DataTrigger
: Trigger is for dependency properties and DataTrigger is for databound properties (data or other controls). Since you are trying to style the Border
, it makes more sense to place the DataTrigger
there and have it watch the TextBox
than to have the TextBox
change the appearance of the Border
.TargetName
property of Setter
is only applicable within a DataTemplate
or ControlTemplate
. (Info from Dr. WPF in this forum post)