0

My issue it that I have created a text box that bolds when someone clicks into it. I want it to unbold when I click somewhere else on the screen. Now heres that hard part I need to do that in my style sheets and the .cs sheet hooked up to style sheet. The contents of my .cs sheet is

using System.Windows;
using System.Windows.Input;
namespace SimTechGUI
{
    public partial class MyResourceDictionary : ResourceDictionary
{ 
   public MyResourceDictionary()
   {
      InitializeComponent();
   }
   private void Window_Focus(object sender, MouseButtonEventArgs e)
   {
       Keyboard.ClearFocus();
   }
}
}

My xaml style sheet looks like

<ResourceDictionary      xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"  
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="SimTechGUI.MyResourceDictionary"
                x:ClassModifier="public">

<Style TargetType="{x:Type Window}">
    <EventSetter Event="MouseDown" Handler="Window_Focus" />
</Style>


<Style TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="MinWidth" Value="120" />
    <Setter Property="MinHeight" Value="25" />
    <Setter Property="AllowDrop" Value="true" />
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border Name="Border" CornerRadius="6" Padding="2" BorderBrush="Black" BorderThickness="2,1">
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderThickness" TargetName="Border" Value="3"/>                            
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

What I have does not currently work. Does anyone know what I have to do to make this work.

netgen
  • 35
  • 9

2 Answers2

0

Your code works fine if you set MouseDown Event Handler directly on Window

MouseDown="Window_Focus"

The way you have the style applied, the Window_Focus Handler never hits. Put a breakpoint on Keyboard.ClearFocus(); and see if it hits?

Noman Khan
  • 92
  • 5
  • It did not hit. Also, I need to do it on the style page that way I don't have to add it every time a new Window is added to the project. – netgen Jun 11 '15 at 19:01
0

You can Apply the Style by Name Explicitly Define a style like this:

        <Style x:Key="windowStyle" TargetType="Window">
            <EventSetter Event="MouseDown" Handler="Window_Focus" />
        </Style>

and Apply like this to all the Windows:

Style="{StaticResource windowStyle}"

The reason I think it's not letting you specify Style Implicitly is probably there is an Style already being applied to all the Windows:

Look at these posts: What reasons could prevent explicit and implicit styles from applying?

or Global Style not working in WPF

Community
  • 1
  • 1
Noman Khan
  • 92
  • 5
  • I want to do it without having to touch the window. Only my style sheet and the .cs I have hooked up to it. I have got it working in the window before even starting the thread I am trying to find a way to do it in the style sheet. – netgen Jun 12 '15 at 15:14