Note that there are some explanatory texts on larger screens.

plurals
  1. POTo remove focus rectangle in WPFwithin a UserControl
    primarykey
    data
    text
    <p>I have a situation where I need to tab using <code>TabKey</code> from one control to another. The condition is that the focus should never go to control that does not have user inputs so only <code>Text</code>, <code>ComboBox</code>, <code>List DatePicker</code> but somehow the focus after 3 controls get the <code>Focus</code> to go to a dashed line <code>Rectangle</code> (Could be of a <code>Grid</code>, StackPanel, I have not been able to findout) around the control groups before it gets into the control. I have searched very thoroughly in Google and stack over flow for a solution but none seem to work.</p> <p>Various solutions I tried:</p> <p>1) Here I set <code>FocusVisualStyle</code> property to null right at start up for <code>Grid</code> and <code>StackPanels</code>. Created a new class:</p> <pre><code>&lt;StackPanel views:FocusVisualTreeChanger.IsChanged="True" Name="parentStackPanel" Orientation="Vertical" Style="{DynamicResource key1}" FocusVisualStyle="{x:Null}"&gt; public class FocusVisualTreeChanger { public static bool GetIsChanged(DependencyObject obj) { return (bool)obj.GetValue(IsChangedProperty); } public static void SetIsChanged(DependencyObject obj, bool value) { obj.SetValue(IsChangedProperty, value); } public static readonly DependencyProperty IsChangedProperty = DependencyProperty.RegisterAttached("IsChanged", typeof(bool), typeof(FocusVisualTreeChanger), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, IsChangedCallback)); private static void IsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (true.Equals(e.NewValue)) { FrameworkContentElement contentElement = d as FrameworkContentElement; if (contentElement != null) { contentElement.FocusVisualStyle = null; return; } FrameworkElement element = d as FrameworkElement; if (element != null) { element.FocusVisualStyle = null; } } } } </code></pre> <p>Did not work. </p> <p>I tried setting <code>FocusVisualStyle</code> property to <em>null</em> for only <code>Grid</code> and <code>StackPanel</code> seems to go through the codebehind if I put a break point but the focus rectangle does not go away:</p> <pre><code>&lt;Grid Name="AppointmentGrid" Style="{DynamicResource key2}" FocusVisualStyle="{x:Null}"&gt; Style style = new Style { TargetType = typeof(Grid) }; style.Setters.Add(new Setter(Grid.FocusVisualStyleProperty, null)); style.Setters.Add(new Setter(Grid.FocusableProperty, false)); Application.Current.Resources["key2"] = style; Application.Current.Resources["key3"] = style; Application.Current.Resources["key4"] = style; Style style1 = new Style { TargetType = typeof(StackPanel) }; style1.Setters.Add(new Setter(StackPanel.FocusVisualStyleProperty, null)); style1.Setters.Add(new Setter(StackPanel.FocusableProperty, false)); Application.Current.Resources["key1"] = style1; </code></pre> <p>Can anyone please help me out with a solution that I have not already tried. None in stackoverflow solutions seem to work. I also set Focusable=false just incase but that doesn't seem to help either.</p> <p>I also read:</p> <p><a href="https://stackoverflow.com/questions/818566/remove-focus-rectangle-on-a-usercontrol">Remove focus rectangle on a UserControl</a></p> <p><a href="https://stackoverflow.com/questions/15783943/wpf-control-remove-focus-and-hover-effect-focusvisualstyle">WPF Control remove focus and hover effect (FocusVisualStyle?!)</a></p> <p><a href="https://stackoverflow.com/questions/592665/wpf-remove-dotted-border-around-focused-item-in-styled-listbox">WPF: Remove dotted border around focused item in styled listbox</a></p> <p>This is what I think I am stuck at. A comment I found in one of the search <a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/141c8bfa-f152-4f8a-aca0-3c3b5440d183/globally-removing-focus-rectangle" rel="nofollow noreferrer">sites</a>.</p> <p>That's a great way to change the default value of a DP, but it will not help in situations where a control's style explicitly changes the property value. Unfortunately, <code>FocusVisualStyle</code> is one such property. More specifically, styles for controls like <code>Button</code>, <code>ComboBox</code>, <code>ListBox</code>, etc. tend to explicitly contain a Setter for the FocusVisualStyle property. This setter will override the default value that you establish by overriding the metadata.</p> <p>Can someone suggest a solution that will work in my case. I have a User control </p> <pre><code>&lt;UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:csla="http://schemas.lhotka.net/4.2.0/xaml" xmlns:input="clr- FocusVisualStyle="{x:Null}" Focusable="False" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" mc:Ignorable="d" d:DesignWidth="948" IsTabStop="False" TabIndex="-1"&gt; &lt;StackPanel views:FocusVisualTreeChanger.IsChanged="True" Name="parentStackPanel" Orientation="Vertical" Style="{DynamicResource key1}" FocusVisualStyle="{x:Null}"&gt;&lt;Grid Name="AppointmentGrid" Style="{DynamicResource key2}" FocusVisualStyle="{x:Null}"&gt; </code></pre> <p>Thanks Dhiren</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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