Search code examples
c#wpfevents

WPF image's gotfocus/lostfocus event does not fired


Could You please help me, why GotFocus and LostFocusa event doesn't fired when I Click to image and then to textbox?

My XAML:

<Window x:Class="imageclick.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <Image Source="Untitled.png" GotFocus="GF" LostFocus="LF" Focusable="True"></Image>
            <TextBox ></TextBox>
        </StackPanel>
    </Grid>
</Window>

I could not understand why GotFocus/LostFocus event never fired

Thanks in advance

Update: When I set the tabindex, when the tab reached the image event fired, but I could not reach with mouse click


Solution

  • Image isn't a Control. Only Controls can get focus.Instead of GotFocus and LostFocus use MouseEnter and MouseLeave events,

     <StackPanel>
                <Image Stretch="Uniform" Source="Untitled.png"   Height="410" MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave"></Image>
                <TextBox Height="65"></TextBox>
     </StackPanel>