当文本框焦点时改变容器的背景颜色

编程入门 行业动态 更新时间:2024-10-24 09:22:25
本文介绍了当文本框焦点时改变容器的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的用户控件,使用 TextBox 。我想在 TextBox 获得焦点时更改用户控件的颜色。这是我有:

< UserControl x:Class =OutLookContactList.ContactSearchControl xmlns =http ://schemas.microsoft/winfx/2006/xaml/presentation xmlns:x =schemas.microsoft/winfx/2006/xamlx: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> 但是我得到了errotTargetName属性不能在样式Setter上设置。当文本框获得焦点时,如何设置用户控件的背景颜色? 感谢你们

解决方案

它是否可以用来包装 UserControl 在边界对象内?如果是这样的话,你可以简单地设置 Border >样式:

< ; UserControl x:Class =Sample2.ContactSearchControl xmlns =schemas.microsoft/winfx/2006/xaml/presentation xmlns:x =http:// schemas .microsoft / 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> 更新:(回答Sheraz的问题)

我不确定为什么 ElementName 不能用于访问 UserControl中的子项。这可能与构建可视化树的方式有关。

至于触发器 vs DataTrigger :触发器 a>用于依赖属性,而 DataTrigger 用于数据绑定属性(data 或其他控件)。既然你正在试图设置 Border 的样式,把 DataTrigger 放在那里更有意义, code> TextBox 比 TextBox 更改 Border的外观 。

据我所知, Targetter 属性 Setter 仅适用于 DataTemplate 或 ControlTemplate 内。 (来自Dr. WPF的信息这个论坛帖子)

I have a simple user control with a 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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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>

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 UserControl inside a Border object? If so, you can simply style the Border like so:

<UserControl x:Class="Sample2.ContactSearchControl" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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>

Update: (Answering Sheraz' Questions)

I'm not sure why ElementName doesn't work for accessing children within a UserControl. It might have something to do with the way the visual tree is constructed.

As for 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.

As I understand it, the TargetName property of Setter is only applicable within a DataTemplate or ControlTemplate. (Info from Dr. WPF in this forum post)

更多推荐

当文本框焦点时改变容器的背景颜色

本文发布于:2023-06-09 04:12:15,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   文本框   颜色   背景   焦点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!