Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Will it work to wrap the contents of your <code>UserControl</code> inside a <code>Border</code> object? If so, you can simply style the <code>Border</code> like so:</p> <pre><code>&lt;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"&gt; &lt;Border&gt; &lt;Border.Style&gt; &lt;Style TargetType="Border"&gt; &lt;Setter Property="Background" Value="White" /&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding IsFocused, ElementName=txtSearch}" Value="true"&gt; &lt;Setter Property="Background" Value="Black" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Border.Style&gt; &lt;StackPanel&gt; &lt;TextBox x:Name="txtSearch" Text="Search" /&gt; &lt;TextBox Text="Other" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/UserControl&gt; </code></pre> <p><strong>Update: (Answering Sheraz' Questions)</strong></p> <p>I'm not sure why <code>ElementName</code> doesn't work for accessing children within a <code>UserControl</code>. It might have something to do with the way the visual tree is constructed.</p> <p>As for <code>Trigger</code> vs <code>DataTrigger</code>: <a href="http://msdn.microsoft.com/en-us/library/system.windows.trigger.aspx" rel="noreferrer">Trigger</a> is for dependency properties and <a href="http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx" rel="noreferrer">DataTrigger</a> is for databound properties (data <em>or</em> other controls). Since you are trying to style the <code>Border</code>, it makes more sense to place the <code>DataTrigger</code> there and have it watch the <code>TextBox</code> than to have the <code>TextBox</code> change the appearance of the <code>Border</code>.</p> <p>As I understand it, the <code>TargetName</code> property of <code>Setter</code> is only applicable within a <code>DataTemplate</code> or <code>ControlTemplate</code>. (<a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b59822de-21ea-4a74-a115-3860241ba6b1" rel="noreferrer">Info from Dr. WPF in this forum post</a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload