Click here to Skip to main content
14,875,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
I want the button to work exactly like when the window first opens and has no focus control.


Before and After:
https://pasteboard.co/JP6CMpt.gif[^]



Thanks

What I have tried:

XML
<Window.Resources>
<LinearGradientBrush x:Key="NormalBackground" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#e9f1cc" Offset="0"/>
            <GradientStop Color="#cbd3ae" Offset="0.5"/>
            <GradientStop Color="#858d68" Offset="1"/>
</LinearGradientBrush>
<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>
</Window.Resources>
<Grid>
<Button Name="UploadButton" PreviewMouseLeftButtonUp="UploadButton_PreviewMouseLeftButtonUp" MouseEnter="UploadButton_MouseEnter" GotFocus="UploadButton_GotFocus" Style="{StaticResource ButtonStyle}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Cursor="Hand" Margin="212,467,0,0">
                <Button.Effect>
                    <DropShadowEffect BlurRadius="3" ShadowDepth="0.1" Direction="-90" Color="#e9f1cc"/>
                </Button.Effect>
</Button>
</Grid>

C#
private void UploadButton_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    FirstName_TextBox.Focus();
    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();
    }
}
Posted
Updated 23-Feb-21 9:06am
v25

It's probably because you're opening the OpenFileDialog from the *preview* event which causes the button to keep fopcus until you close the OpenFileDialog.

Try using the button's Click event instead.
   
Comments
Reza jafery 19-Feb-21 9:07am
   
I try this but it didn't work.
#realJSOP 19-Feb-21 9:46am
   
Change your question to show your XAML
Reza jafery 19-Feb-21 10:53am
   
XAML codes added ( Look at the codes above ) .Now what is your solution?
#realJSOP 19-Feb-21 11:07am
   
Where is your Button XAML? And have you tried it without your storyboard animations?
Reza jafery 19-Feb-21 11:28am
   
Button codes added ( Look at the codes above ).
I need to run STORYBOARD to make the button look beautiful.
Reza jafery 19-Feb-21 11:38am
   
Dear friend , if all the focus of the window is like the window load time , the problem will be solved ( because in The load event , everything works Properly , but when I click on the button , the focus changes ).
#realJSOP 19-Feb-21 16:38pm
   
So after the FileDialog returns, call this.Focus() and see what happens.

FWIW, "pretty" is a waste of CPU cycles.
Reza jafery 21-Feb-21 13:00pm
   
I did this and it didn't work.
Reza jafery 19-Feb-21 13:20pm
   
https://pasteboard.co/JP6CMpt.gif
I found the solution:

XML
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"></Setter>


Details are in this link: https://docs.microsoft.com/en-us/answers/questions/279481/the-ismouseover-attribute-stays-in-enteractionsexi.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