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

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

问题描述:

我有一个简单的用户控件,使用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="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> 

但我得到了errot“TargetName属性不能在风格设置器上设置”。如何在文本框获得焦点时设置用户控件的背景颜色? 多谢

它是否会将UserControl的内容包装到Border对象中?如果是这样,你可以简单的样式像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="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> 

更新:(回答Sheraz”问题)

我不知道为什么ElementName不为UserControl中访问儿童工作。这可能与构建可视化树的方式有关。

至于Trigger VS DataTriggerTrigger为依赖属性和DataTrigger为数据绑定属性(数据或其它控制)。由于您试图对Border进行样式设计,因此将DataTrigger放置在那里并让它看TextBox比让TextBox更改Border的外观更有意义。

据我所知,SetterTargetName属性只适用于DataTemplateControlTemplate。 (Info from Dr. WPF in this forum post

+0

是的,它的作品。非常感谢约瑟夫。 有几个问题。 为什么它不适用于UserControl并且适用于Border? 你怎么知道它是需要使用的DataTrigger,因为我的假设是DataTrigger是使用数据而不是控件,而且我一直试着触发器(而不是DataTrigger) – Sheraz 2009-09-18 15:46:50

+1

请参阅我的回答。 – 2009-09-18 16:49:20

如果你改变了你需要删除TargetName属性中的文本框的背景:

<Style x:Key="searchTextBoxStyle" TargetType="{x:Type TextBox}"> 
    <Style.Triggers> 
     <Trigger Property="IsFocused" Value="true"> 
      <Setter Property="Background" Value="{StaticResource OnMouseOverColor}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

,并更改希望将TextBox这种风格是:

<TextBox Style="{StaticResource searchTextBoxStyle}" .... /> 

但是,由于您想要更改父级用户控件的值,因此不会给您想要的值。

你当然可以在后面的代码中添加一个GotFocus事件处理程序,并将代码更改为背景颜色。

+0

但这不会设置用户控件文本框的背景。我想在文本框获得焦点时更改用户控件的颜色。 – Sheraz 2009-09-17 20:23:19

+0

更改代码有额外的维护功能,因为我不仅需要在对焦时设置颜色,还需要在失去焦点时恢复正常(两倍的工作量)。这就是为什么触发器是最好的,因为当Setter值不再真实时它会自动重置属性。 – Sheraz 2009-09-18 01:42:37

下面是一些XAML,在Kaxaml工作:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Page.Style> 
    <Style TargetType="Page"> 
     <Setter Property="Background" Value="#CCCCD0" /> 
     <Style.Triggers> 
     <DataTrigger Binding="{Binding ElementName=txtSearch, Path=IsFocused}" 
        Value="true"> 
      <Setter Property="Background" Value="Black" /> 
     </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    </Page.Style> 

    <TextBox x:Name="txtSearch" Width="100" 
      HorizontalAlignment="Center" VerticalAlignment="Center" /> 

</Page> 

你会改变Page对象与你UserControl。我发现在编码起始号为UserControl的VS之前,在诸如Kaxaml之类的快速原型开发工具中测试这些事情要容易得多。

请注意,您必须通过属性设置器而不是通过Page本身的属性来设置默认颜色(本例中为#CCCCD0)。这是因为该属性会覆盖由触发器设置的值(因为它是一个样式触发器),所以即使触发器会触发,它总是会被本地属性规范所取代,这意味着它不会改变。我只指出这一点,因为这是一个相当普遍的问题。

+0

这在Kazaml中工作正常,但它不适用于IDE。奇怪的。这是输出窗口中的例外 System.Windows.Data错误:4:找不到与参考'ElementName = SearchTextBox'绑定的源。 BindingExpression:路径= IsFocused;的DataItem = NULL;目标元素是'ContactSearchControl'(Name ='root');目标属性是'NoTarget'(键入'Object') 显然它无法找到SearchTextBox。如果我将xaml粘贴到Kaxaml中,它可以正常工作。它似乎与嵌套控件有关,因为我在另一个用户控件中使用该用户控件。 – Sheraz 2009-09-17 21:20:19