Mirza Bilal
Mirza Bilal

Reputation: 1050

Detect Mouseover on some inner control inside another control in wpf

I am creating a custom TexBox with Button and a Label inside it. I want to detect the mouseover on button and change the value of Label and do some more stuff. Way i know to this is

BUT this does not sound cute, and if there is something that WPF provides which i dont know about?

<ControlTemplate TargetType="{x:Type TextBox}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="26"/>
        </Grid.ColumnDefinitions>
        <Border x:Name="bg" BorderBrush="#d9dce1" BorderThickness="0"/>
        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Height="26"  />
        <TextBlock x:Name="textBlock" Opacity="0.5"  Padding="4,0,0,0" TextWrapping="Wrap" Visibility="Hidden"  VerticalAlignment="Center"/>
        <StackPanel Grid.Column="1" Width="26" Orientation="Horizontal">
            <Line  DockPanel.Dock="Right" X1="0" Y1="0" X2="0" Y2="26" Margin="0" Stroke="#FFD9DCE1" StrokeThickness="1" Width="1"/>
            <Button x:Name="keyBtn" Content="↩" HorizontalContentAlignment="Center" Height="26" Width="26" Background="#f9f9f9" BorderThickness="0" Click="Button_Click">
            </Button>
        </StackPanel>
    </Grid>
    <ControlTemplate.Triggers>

        <!-- Detect Mouseover on button here and set some values -->

        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="Text" Value="" />
                <Condition Property="IsFocused" Value="False" />
            </MultiTrigger.Conditions>
            <Setter Property="Visibility" TargetName="textBlock" Value="Visible" />
            <Setter TargetName="textBlock" Property="Text" Value="Click to record shortcut" />
            <Setter Property="Foreground" Value="Black" />
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="Text" Value="" />
                <Condition Property="IsFocused" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Visibility" TargetName="textBlock" Value="Visible" />
            <Setter TargetName="textBlock" Property="Text" Value="Type shortcut" />
            <Setter Property="Foreground" Value="Red" />
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Generalizing the question asked.

How can we detect mouseover or other event of some inner control which is created in control template?

Upvotes: 0

Views: 2350

Answers (1)

XAMeLi
XAMeLi

Reputation: 6289

Try this:

<Trigger SourceName="keyBtn"
         Property="IsMouseOver"
         Value="True">
    <!--Do stuff here-->
</Trigger>

Insert this instead of the comment in your snippet, i.e. in control template triggers.

Upvotes: 1

Related Questions