Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of Mutsop
Mutsop🇧🇪

MenuItem Style on mouseover - WPF
Hi,

I'm trying to change the styling of when someone mouseover a menuItem (in xaml)
<Menu Height="30" HorizontalAlignment="Center" Name="MenuTools" VerticalAlignment="Center" Width="360" Background="{x:Null}">
                <MenuItem Name="New">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/new.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="Save">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/save.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="Open">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/open.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="Cut">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/cut.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="Paste">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/paste.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="Settings">
                    <MenuItem.Icon>
                        <Image Source="/Chalkboard_wpf;component/Resources/settings.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Style>
                    <Style.Triggers>
                        <Trigger Property="menuItem.IsMouseOver" Value="True">
                            <Setter Property="MenuItem.Background" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Menu>

Open in new window


I tried to add a parent "MenuItem.Style" to the "Style" element, but it apparently doesnt exist.

Any Ideas?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


SOLUTION
Avatar of Nasir RazzaqNasir Razzaq🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of MutsopMutsop🇧🇪

ASKER

K so after reading Tao Liang's answer I tried adding the following:
<Window.Resources>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

Open in new window


But my menuitems just disappeared.

Avatar of MikeTooleMikeToole🇬🇧

The code you posted will, I think, replace the whole of the control's visual template - with a template that has no visible components!

Avatar of MutsopMutsop🇧🇪

ASKER

So i need to redesign the whole thing? Just for hovering styling :D

Styling is harder than I thought

Is there maybe another way? Behind code maybe?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of MikeTooleMikeToole🇬🇧

As far as I'm aware that's it - copy the existing template and tweak it.
The Windows SDK has XAML files with all the themes styles for each control.
There are also ways of retrieving the style for a control in code, but I'd have to look them up.

Avatar of MutsopMutsop🇧🇪

ASKER

Where do you find these?
"The Windows SDK has XAML files with all the themes styles for each control."


I also tried adding
BasedOn="{StaticResource {x:Type MenuItem}}"

Open in new window


But I think it overrides everything yet again :(

ASKER CERTIFIED SOLUTION
Avatar of MikeTooleMikeToole🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.