How Can I make IsFocused false when there MouseOver is True?

MERUN KUMAR MAITY 636 Reputation points
2022-12-14T16:48:44.363+00:00

This is my attach behavior for IsFocused. Can someone tell me? How can I make IsFocused false when there MouseOver is True for a WPF control?

 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));  
  
        private static void OnIsFocusedPropertyChanged(  
            DependencyObject d,  
            DependencyPropertyChangedEventArgs e)  
        {  
            var uie = (UIElement)d;  
            if ((bool)e.NewValue)  
            {  
  
                if (uie.IsMouseOver == false)   
                {  
                uie.Focus();      
                }  
  
                //uie.IsMouseOver  
              /*  uie.Focus();*/ // Don't care about false values.  
            }  
        }  
    }  
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-12-15T09:00:12.427+00:00

    You could try to refer to the code below.

    Xaml:

    <Window.DataContext >  
            <local:ViewModel  />  
        </Window.DataContext>  
      
      <Grid>  
            <ComboBox local:FocusExtension.IsFocused="{Binding IsFocused}" ItemsSource="{Binding List}" Height="80" SelectedIndex=" 0">  
                <ComboBox.Style>  
                    <Style TargetType="ComboBox">  
                        <Setter Property="Foreground" Value="Red" />  
                        <Style.Triggers>  
                            <Trigger Property="IsFocused" Value="True">  
                                <Setter Property="Foreground" Value="Green" />  
                            </Trigger>  
                        </Style.Triggers>  
                    </Style>  
                </ComboBox.Style>  
            </ComboBox>  
      
        </Grid>  
    

    Codebehind:

     public class ViewModel   
        {  
            public ObservableCollection<string> List{get;set;} = new ObservableCollection<string>();  
            public ViewModel()  
            {  
                List.Add( "user1" );  
                List.Add( "user2" );  
                List.Add( "user3" );  
                List.Add( "user4" );  
                
            }  
        }  
    

    270935-image.png

    The result:
    270991-4.gif

    Update:

    The complete code:

    271287-comboboxforegroundtrigger.txt

    The result:
    271254-6.gif

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.