Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:30:10+00:00 2026-05-24T17:30:10+00:00

I have some code to set the focused property of a text box, but

  • 0

I have some code to set the focused property of a text box, but what i’m actually after is finding out if the text box currently has the keyboard focus, I need to determine this from my view model

public static class FocusExtension
{
    public static bool GetIsFocused(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFocusedProperty, value);
    }

    public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached
    (
        "IsFocused",
        typeof(bool),
        typeof(FocusExtension),
        new UIPropertyMetadata(false, OnIsFocusedPropertyChanged)
    );

    public static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;
        if ((bool)e.NewValue)
        {
            uie.Focus();
        }
    }
}

And the xaml is

<TextBox Text="{Binding Path=ClientCode}" c:FocusExtension.IsFocused="{Binding IsClientCodeFocused}" />

source of code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:30:11+00:00Added an answer on May 24, 2026 at 5:30 pm

    Edit

    Based on the comments below, here’s an example of an attached property that hooks up an event and updates the source of a binding. I’ll add comments where I know you’ll need to make modifications. Hopefully it will point you in the right direction

    public class TextBoxHelper
    {
        // I excluded the generic stuff, but the property is called 
        // EnterUpdatesSource and it makes a TextBox update it's source
        // whenever the Enter key is pressed
    
        // Property Changed Event - You have this in your class above
        private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            UIElement sender = obj as UIElement;
            if (obj != null)
            {
                // In my case, the True/False value just determined a behavior,
                // so toggling true/false added/removed an event.
    
                // Since you want your events to be on at all times, you'll either
                // want to have two AttachedProperties (one to tell the control
                // that it should be tracking the current focused state, and 
                // another for binding the actual focused state), or you'll want 
                // to find a way to only add the EventHandler when the 
                // AttachedProperty is first added and not toggle it on/off as focus 
                // changes or add it repeatedly whenever this value is set to true
    
                // You can use the GotFocus and LostFocus Events
                if ((bool)e.NewValue == true)
                {
                    sender.PreviewKeyDown += new KeyEventHandler(OnPreviewKeyDownUpdateSourceIfEnter);
                }
                else
                {
                    sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
                }
            }
        }
    
        // This is the EventHandler
        static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
        {
            // You won't need this
            if (e.Key == Key.Enter)
            {
                // or this
                if (GetEnterUpdatesTextSource((DependencyObject)sender))
                {
                    // But you'll want to take this bit and modify it so it actually 
                    // provides a value to the Source based on UIElement.IsFocused
                    UIElement obj = sender as UIElement;
    
                    // If you go with two AttachedProperties, this binding should 
                    // point to the property that contains the IsFocused value
                    BindingExpression textBinding = BindingOperations.GetBindingExpression(
                        obj, TextBox.TextProperty);
    
                    // I know you can specify a value for a binding source, but
                    // I can't remember the exact syntax for it right now
                    if (textBinding != null)
                        textBinding.UpdateSource();
                }
            }
        }
    

    There might be a better way of accomplishing what you’re trying to do, but if not then I hope this provides a good starting point 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have some code I am trying to refactor. A large set of
I have some code that looks like this: std::set<int> s1, s2, out; // ...
In a recent project, I have to maintain some PHP code. I set up
I have java script code to set some of the properties of ajax controls.
I have some basic pageview incrementing code set up on a LAMP server (see
I have some code that calls an API and gets a set of results
I have some code like this: Update table_name set [column] = case when d.data
I have some legacy code that compiles with both -02 and -03 set. From
I have some code (that is working), but I just want to make sure
I have some code that produces a set of primary key values that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.