<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF]]></title><description><![CDATA[<p dir="auto">כאשר עובדים עם TextBox ב-WPF, לפעמים נרצה לשפר את חוויית המשתמש על ידי ביצוע פעולות אוטומטיות, כמו בחירת כל הטקסט כאשר הרכיב מקבל פוקוס או קביעה אוטומטית של פוקוס על TextBox בעת הטעינה.</p>
<p dir="auto">אשמח לקבל הערות והארות.</p>
<p dir="auto">מצו"ב הקוד:</p>
<pre><code>public class TextBoxFocusBehavior
{
    public static bool GetSelectAll(FrameworkElement frameworkElement)
    {
        return (bool)frameworkElement.GetValue(SelectAllProperty);
    }

    public static void SetSelectAll(FrameworkElement frameworkElement, bool value)
    {
        frameworkElement.SetValue(SelectAllProperty, value);
    }

    public static bool GetCaptureFocus(FrameworkElement frameworkElement)
    {
        return (bool)frameworkElement.GetValue(CaptureFocusProperty);
    }

    public static void SetCaptureFocus(FrameworkElement frameworkElement, bool value)
    {
        frameworkElement.SetValue(CaptureFocusProperty, value);
    }

    public static readonly DependencyProperty CaptureFocusProperty =
        DependencyProperty.RegisterAttached("CaptureFocus",
            typeof(bool), typeof(TextBoxFocusBehavior),
            new FrameworkPropertyMetadata(false, OnCaptureFocusChanged));

    public static readonly DependencyProperty SelectAllProperty =
             DependencyProperty.RegisterAttached("SelectAll",
                typeof(bool), typeof(TextBoxFocusBehavior),
                new FrameworkPropertyMetadata(false, OnSelectAllChanged));

    private static void OnSelectAllChanged
               (DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement == null) return;

        if (e.NewValue is bool == false) return;

        if ((bool)e.NewValue)
        {
            frameworkElement.GotFocus += SelectAll;
            frameworkElement.PreviewMouseDown += IgnoreMouseButton;
        }
        else
        {
            frameworkElement.GotFocus -= SelectAll;
            frameworkElement.PreviewMouseDown -= IgnoreMouseButton;
        }
    }

    private static void OnCaptureFocusChanged
              (DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement == null) return;

        if (e.NewValue is bool == false) return;

        if ((bool)e.NewValue)
        {
            frameworkElement.Loaded += FrameworkElement_Loaded;
        }
        else
        {
            frameworkElement.Loaded -= FrameworkElement_Loaded;
        }
    }

    private static void FrameworkElement_Loaded(object sender, RoutedEventArgs e)
    {
        var frameworkElement = e.OriginalSource as FrameworkElement;
        frameworkElement.Focus();
    }

    private static void SelectAll(object sender, RoutedEventArgs e)
    {
        var frameworkElement = e.OriginalSource as FrameworkElement;
        if (frameworkElement is TextBox)
            ((TextBoxBase)frameworkElement).SelectAll();
        else if (frameworkElement is PasswordBox)
            ((PasswordBox)frameworkElement).SelectAll();
    }

    private static void IgnoreMouseButton
            (object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var frameworkElement = sender as FrameworkElement;
        if (frameworkElement == null || frameworkElement.IsKeyboardFocusWithin) return;
        e.Handled = true;
        frameworkElement.Focus();
    }
}
</code></pre>
<p dir="auto">המחלקה TextBoxFocusBehavior מספקת שתי יכולות:</p>
<pre><code>Select All: בחירת כל הטקסט כאשר הרכיב מקבל פוקוס.
Capture Focus: קביעה כי רכיב מסוים יקבל פוקוס אוטומטית בעת טעינת החלון.
</code></pre>
<p dir="auto">דוגמאות שימוש:</p>
<pre><code>&lt;TextBox Text="Hello, World!" 
         local:TextBoxFocusBehavior.SelectAll="True" 
         local:TextBoxFocusBehavior.CaptureFocus="True" /&gt;
</code></pre>
]]></description><link>https://tchumim.com/topic/16767/textboxfocusbehavior-לשיפור-חוויית-המשתמש-ב-wpf</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Jul 2026 19:41:46 GMT</lastBuildDate><atom:link href="https://tchumim.com/topic/16767.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Dec 2024 22:27:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF on Tue, 10 Dec 2024 12:44:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ivrtikshoret">@<bdi>ivrtikshoret</bdi></a><br />
תודה שעלית את השאלה הזו<br />
אני מאד אוהב לשתף ולעזור אך תהיתי לעצמי לא פעם אם יש בזה תועלת למישהו<br />
היה חסר לי confirmation תודה.<br />
חוץ מזה אני מאוד נבניתי מהפורום כאן הערה פה הערה שם של אנשים טובים על הקודים שלי נתנו לי המון ידע.</p>
]]></description><link>https://tchumim.com/post/165094</link><guid isPermaLink="true">https://tchumim.com/post/165094</guid><dc:creator><![CDATA[pcinfogmach]]></dc:creator><pubDate>Tue, 10 Dec 2024 12:44:06 GMT</pubDate></item><item><title><![CDATA[Reply to TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF on Tue, 10 Dec 2024 11:01:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/thmv">@<bdi>THMV</bdi></a> סליחה, אתה נמצא בקטגוריית תכנות, וכמדומני שאינך מתכנת.</p>
]]></description><link>https://tchumim.com/post/165089</link><guid isPermaLink="true">https://tchumim.com/post/165089</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Tue, 10 Dec 2024 11:01:16 GMT</pubDate></item><item><title><![CDATA[Reply to TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF on Tue, 10 Dec 2024 10:40:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pcinfogmach">@<bdi>pcinfogmach</bdi></a> אני אישית גם מצטרף לזה שטוב שמעלים פה כאלה דברים אבל שיהיו יותר ברורים למי שלא מבין את כל המושגים שציטטת לדוגמא</p>
<blockquote>
<p dir="auto">טקסטבוקסים או רכיבים אחרים שמכילים טקסט ב-WPF</p>
</blockquote>
]]></description><link>https://tchumim.com/post/165088</link><guid isPermaLink="true">https://tchumim.com/post/165088</guid><dc:creator><![CDATA[THMV]]></dc:creator><pubDate>Tue, 10 Dec 2024 10:40:56 GMT</pubDate></item><item><title><![CDATA[Reply to TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF on Tue, 10 Dec 2024 08:29:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ivrtikshoret">@<bdi>ivrtikshoret</bdi></a><br />
<strong>כן<br />
מאוד!</strong></p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pcinfogmach">@<bdi>pcinfogmach</bdi></a> מעלה פתרונות נהדרים לכל מיני דברים<br />
שאנחנו כמפתחים קטנים שלא למדנו מסודר את כל השפה מיסודה ולא בקיאים בה<br />
מוצאים את עצמינו נעזרים בזה</p>
<p dir="auto">אשמח מאוד שתמשיך לעלות מגוון קודים ורעיונות מעין אלו!</p>
<p dir="auto">--<br />
נ.ב. זה כמובן מעבר לעובדה שמדובר בפורום ציבורי<br />
שזה אומר<br />
שמצד אחד - אין לך חובה לקרוא כל הגיג שעלה<br />
מצד שני - זכותו של כל אנונימי לעלות תכנים כמה שיחפוץ, כל עוד זה עומד בכללי הפורום</p>
]]></description><link>https://tchumim.com/post/165087</link><guid isPermaLink="true">https://tchumim.com/post/165087</guid><dc:creator><![CDATA[mekev]]></dc:creator><pubDate>Tue, 10 Dec 2024 08:29:55 GMT</pubDate></item><item><title><![CDATA[Reply to TextBoxFocusBehavior - לשיפור חוויית המשתמש ב-WPF on Tue, 10 Dec 2024 07:48:16 GMT]]></title><description><![CDATA[<p dir="auto">סליחה על השאלה הישירה..<br />
זה רלוונטי למישהו כל הקודים שאתה מעלה לפה?</p>
]]></description><link>https://tchumim.com/post/165086</link><guid isPermaLink="true">https://tchumim.com/post/165086</guid><dc:creator><![CDATA[ivrtikshoret]]></dc:creator><pubDate>Tue, 10 Dec 2024 07:48:16 GMT</pubDate></item></channel></rss>