question

64936295 avatar image
0 Votes"
64936295 asked ·

The 'ismouseover' attribute stays in 'enteractions/exitactions' mode , how do I change the focus of the button ? please note the image (GIF) that shows how it works.

Hi to all.
First of all I am sorry for my language grammar because my first language is Persian (Iran). I created a button that has a series of effects and these effects work properly when the WPF window opens. But when the PreviewMouseLeftButtonUp event is done and the Openfiledialog window opens, the button stays in focus mode (the IsMouseOver attribute stays in EnterActions mode). When I shift the focus of the button to another control, the IsMouseOver feature stays in ExitActions mode.

https://pasteboard.co/JP6CMpt.gif

I want the button to work exactly like when the window first opens and has no focus control.

 <Storyboard x:Key="ButtonMouseEnter" AutoReverse="False" BeginTime="0:0:0"  SpeedRatio="3">
         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="Background">
             <DiscreteObjectKeyFrame KeyTime="0:0:0.0">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#e9f1cc" Offset="0"/>
                         <GradientStop Color="#cbd3ae" Offset="0.5"/>
                         <GradientStop Color="#858d68" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#dfe7c2" Offset="0"/>
                         <GradientStop Color="#c8d0ab" Offset="0.5"/>
                         <GradientStop Color="#8f9772" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.2">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#d5ddb8" Offset="0"/>
                         <GradientStop Color="#c5cda8" Offset="0.5"/>
                         <GradientStop Color="#99a17c" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#cbd3ae" Offset="0"/>
                         <GradientStop Color="#c2caa5" Offset="0.5"/>
                         <GradientStop Color="#a3ab86" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.4">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#c1c9a4" Offset="0"/>
                         <GradientStop Color="#bfc7a2" Offset="0.5"/>
                         <GradientStop Color="#adb590" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#b7bf9a" Offset="0"/>
                         <GradientStop Color="#bcc49f" Offset="0.5"/>
                         <GradientStop Color="#b7bf9a" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#adb590" Offset="0"/>
                         <GradientStop Color="#b9c19c" Offset="0.5"/>
                         <GradientStop Color="#c1c9a4" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.7">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#a3ab86" Offset="0"/>
                         <GradientStop Color="#b6be99" Offset="0.5"/>
                         <GradientStop Color="#cbd3ae" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.8">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#99a17c" Offset="0"/>
                         <GradientStop Color="#b3bb96" Offset="0.5"/>
                         <GradientStop Color="#d5ddb8" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.9">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#8f9772" Offset="0"/>
                         <GradientStop Color="#b0b893" Offset="0.5"/>
                         <GradientStop Color="#dfe7c2" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:1.0">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#858d68" Offset="0"/>
                         <GradientStop Color="#adb590" Offset="0.5"/>
                         <GradientStop Color="#e9f1cc" Offset="1"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
         </ObjectAnimationUsingKeyFrames>
     </Storyboard>
     <Storyboard x:Key="ButtonMouseLeave" AutoReverse="False" BeginTime="0:0:0"  SpeedRatio="3">
         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="Background">
             <DiscreteObjectKeyFrame KeyTime="0:0:0.0">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#e9f1cc" Offset="1"/>
                         <GradientStop Color="#adb590" Offset="0.5"/>
                         <GradientStop Color="#858d68" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#dfe7c2" Offset="1"/>
                         <GradientStop Color="#b0b893" Offset="0.5"/>
                         <GradientStop Color="#8f9772" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.2">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#d5ddb8" Offset="1"/>
                         <GradientStop Color="#b3bb96" Offset="0.5"/>
                         <GradientStop Color="#99a17c" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#cbd3ae" Offset="1"/>
                         <GradientStop Color="#b6be99" Offset="0.5"/>
                         <GradientStop Color="#a3ab86" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.4">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#c1c9a4" Offset="1"/>
                         <GradientStop Color="#b9c19c" Offset="0.5"/>
                         <GradientStop Color="#adb590" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#b7bf9a" Offset="1"/>
                         <GradientStop Color="#bcc49f" Offset="0.5"/>
                         <GradientStop Color="#b7bf9a" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#adb590" Offset="1"/>
                         <GradientStop Color="#bfc7a2" Offset="0.5"/>
                         <GradientStop Color="#c1c9a4" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.7">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#a3ab86" Offset="1"/>
                         <GradientStop Color="#c2caa5" Offset="0.5"/>
                         <GradientStop Color="#cbd3ae" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.8">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#99a17c" Offset="1"/>
                         <GradientStop Color="#c5cda8" Offset="0.5"/>
                         <GradientStop Color="#d5ddb8" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:0.9">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#8f9772" Offset="1"/>
                         <GradientStop Color="#c8d0ab" Offset="0.5"/>
                         <GradientStop Color="#dfe7c2" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
             <DiscreteObjectKeyFrame KeyTime="0:0:1.0">
                 <DiscreteObjectKeyFrame.Value>
                     <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                         <GradientStop Color="#858d68" Offset="1"/>
                         <GradientStop Color="#cbd3ae" Offset="0.5"/>
                         <GradientStop Color="#e9f1cc" Offset="0"/>
                     </LinearGradientBrush>
                 </DiscreteObjectKeyFrame.Value>
             </DiscreteObjectKeyFrame>
         </ObjectAnimationUsingKeyFrames>
     </Storyboard>
     <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
         <Setter Property="BorderBrush" Value="#FF707070" />
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
         <Setter Property="HorizontalContentAlignment" Value="Center"/>
         <Setter Property="VerticalContentAlignment" Value="Center"/>
         <Setter Property="Padding" Value="1"/>
         <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="{x:Type Button}">
                     <Border Width="69" Background="{StaticResource NormalBackground}"
             BorderBrush="#d2d8ca" BorderThickness="0.6" CornerRadius="15" x:Name="Button">
                         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                     Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                     </Border>
                     <ControlTemplate.Triggers>
                         <Trigger Property="IsMouseOver" Value="true">
                             <Trigger.EnterActions>
                                 <BeginStoryboard Storyboard="{StaticResource ButtonMouseEnter}"/>
                             </Trigger.EnterActions>
                             <Trigger.ExitActions>
                                 <BeginStoryboard Storyboard="{StaticResource ButtonMouseLeave}"/>
                             </Trigger.ExitActions>
                         </Trigger>
                         <Trigger Property="IsFocused" Value="true">
                             <Trigger.EnterActions>
                                 <BeginStoryboard Storyboard="{StaticResource ButtonMouseEnter}"/>
                             </Trigger.EnterActions>
                             <Trigger.ExitActions>
                                 <BeginStoryboard Storyboard="{StaticResource ButtonMouseLeave}"/>
                             </Trigger.ExitActions>
                         </Trigger>
                     </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>


     private void UploadButton_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
     {
         Keyboard.ClearFocus();
         System.Windows.Forms.OpenFileDialog OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
         OpenFileDialog.AutoUpgradeEnabled = false;
         if (App.EnumLanguage.Equals(AllLanguage.English))
         {
             OpenFileDialog.Title = "Selecting Image";
         }
         else
         {
             OpenFileDialog.Title = "انتخاب تصویر";
         }
         OpenFileDialog.Filter = "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp|GIF(*.gif)|*.gif|PNG(*.png)|*.png|All Files|*.*";
         OpenFileDialog.ShowDialog();
         if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             var IMG = System.Drawing.Image.FromFile(OpenFileDialog.FileName);
             BM = new BitmapImage(new Uri(OpenFileDialog.FileName));
             BitmapImage BitMapImage = new BitmapImage();
             BitMapImage.BeginInit();
             System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();
             IMG.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
             MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);
             BitMapImage.StreamSource = MemoryStream;
             BitMapImage.EndInit();
             MemberImage.Source = BitMapImage;
             OpenFileDialog.Dispose();
         }
     }

Thanks














dotnet-csharpdotnet-wpf-xaml
before.jpg (58.7 KiB)
after.jpg (172.6 KiB)
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@64936295
Do you want to click the Button which has been clicked to show openFileDialog? Or just want to keep Button's style? Did you set custom style for your Button?If you did, please give me some code snippet?By the way, could you re-upload your images for me to analzye your question?

0 Votes 0 ·

I say again.
I want the button to work exactly like when the window first opens ( because works properly at first ) and has no focus control.

0 Votes 0 ·

This is because it initially has no control over the focus window.
How can I clear all the focus?
I did clear Focus but it didn't work (Keyboard.ClearFocus();).

0 Votes 0 ·

1 Answer

DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

Insert the following code between line 29 and line 30:

 var scope = FocusManager.GetFocusScope(sender as Button); 
 FocusManager.SetFocusedElement(scope, null); 
 Keyboard.ClearFocus();

And add below <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"></Setter> between line 224 and line 225 in your firsrt code part for the question.
70961-2.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.

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It does not work like before OpenFileDialog opened ( Pay attention to the gif image )

70766-mouseover.gif


0 Votes 0 ·
mouseover.gif (819.5 KiB)

@64936295
I had merged the replies to one answer, please check.

0 Votes 0 ·