Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an Attach behavior :

C#
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)
           {
               // Check if the MouseOver property is true.
               if (uie.IsMouseOver)
               {
                   // Set the IsFocused property to false.
                   uie.SetValue(IsFocusedProperty, false);
               }
               else
               {
                   // Otherwise, set focus to the control.
                   uie.Focus();
               }
           }
       }
   }


This is how I use it in my MainWindow.xaml :

C#
<grid>
         <combobox local:focusextension.isfocused="{Binding IsFocused}" itemssource="{Binding List}" height="80" selectedindex=" 0" datacontext="{Binding RelativeSource={RelativeSource Self}}">
             <combobox.style>
                 
                     <Setter Property="Foreground" Value="Red" />
                     <Style.Triggers>
                         <Trigger Property="IsFocused" Value="True">
                             <Setter Property="Foreground" Value="Green" />
                         </trigger>
                     </style.Triggers>
                 
             
         
    
     

unfortunately, my attach behavior not working, Can someone tell me How Can I solve this problem?

What I have tried:

This is the code what I tried..it's very difficult for me to implement attach behavior here.
Posted
Updated 18-Dec-22 22:36pm
Comments
Richard MacCutchan 17-Dec-22 5:21am    
What does "not working" mean? Please use the Improve question link above, and add complete details of what is actually happening.
Member 15061773 17-Dec-22 14:16pm    
According to the attach behavior, the IsFocus should false when mouseover true but I don't get this effect in my Combobox.

If you want to see the full source code then I already post on Microsoft Question and answer forum. I also post required GIF image there.

Here is the link : https://learn.microsoft.com/en-us/answers/questions/1092033/how-to-change-foreground-color-of-a-combobox-on-se.html

Graeme_Grant 17-Dec-22 18:57pm    
HuiLiu-MSFT (on Microsoft forums question, link above) appears to have answered your question. Looking at his gif, it matches your "this is what I want" gif.
Member 15061773 18-Dec-22 3:27am    
His, answer is wrong if you read all comments then you clearly understand. His code has Data Context error, though I solve this problem later by doing datacontext="{Binding RelativeSource={RelativeSource Self}}" this data binding.

But still I don't get the desire effect I want.

Can you please solve my question?
Graeme_Grant 18-Dec-22 5:08am    
Did you set breakpoints inside the FocusExtension methods? What happened when you ran the code?

1 solution

1) You can't set the UIElement.IsFocused property[^]; it's read-only.

2) Your style triggers bind to the UIElement.IsFocused property, not your custom FocusExtension.IsFocused property.

3) Your OnIsFocusedPropertyChanged method will only be called when the FocusExtension.IsFocused property is changed. Since you've bound that to the UIElement.IsFocused property, it will be called when the control gains or loses focus. So your custom property will only be set to false when the control gains focus whilst the mouse is directly over it.

This seems like an XY problem[^]; rather than telling us what problem you're actually trying to solve, you're asking us to help you fix a problem with your chosen solution to the original problem.

If you're just trying to change the style when the mouse is over the control, then use a trigger:
XAML
<ComboBox.Style>
    <Setter Property="Foreground" Value="Red" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Foreground" Value="Green" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>

Otherwise, you'll need to explain precisely what you are trying to do.
 
Share this answer
 
Comments
Member 15061773 19-Dec-22 5:18am    
Thank you very much sir, for your crystal clear explanation. Your code does not solve my problem.

Here is the question link where I described each and every part of the question is clearly explain which I asked on Microsoft Question and Answer Forum :

https://learn.microsoft.com/en-us/answers/questions/1092033/how-to-change-foreground-color-of-a-combobox-on-se.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900