I have a Button whose XAML is like this
<Button x:Name="Button2" Style="{StaticResource User_Hyperlink_Button}" Tag="123456789.XAML" Width="86" Height="30" Margin="26,327,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="Button2" />
The style for the the Button is defined in the ResourceDictionary like this
<Style x:Key="User_Hyperlink_Button" TargetType="{x:Type Button}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Button Style="{DynamicResource App_button}" >
<Hyperlink Style="{DynamicResource Button_LinkForeground}" NavigateUri="{Binding Path=Tag, RelativeSource={RelativeSource Mode=TemplatedParent, AncestorLevel=0}}">
<InlineUIContainer>
<TextBlock Text="{Binding Path=Content, RelativeSource={RelativeSource Mode=TemplatedParent, AncestorLevel=0}}" />
</InlineUIContainer>
</Hyperlink>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In this everything is working fine, but when I do the same for MenuItem the text for the MenuItem dissappears.
The xaml for MenuItem is like this
<MenuItem x:Name="MenuItem1" Style="{StaticResource User_Hyperlink_MenuItem}" Tag="123456789.XAML" Header="MenuItem1" />
The style is like this
<Style x:Key="User_Hyperlink_MenuItem" TargetType="{x:Type MenuItem}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<MenuItem Style="{DynamicResource App_MenuItem}" >
<Hyperlink Style="{DynamicResource Menu_LinkForeground}" NavigateUri="{Binding Path=Tag, RelativeSource={RelativeSource Mode=TemplatedParent, AncestorLevel=0}}">
<InlineUIContainer>
<TextBlock Text="{Binding Path=Header, RelativeSource={RelativeSource Mode=TemplatedParent, AncestorLevel=0}}" />
</InlineUIContainer>
</Hyperlink>
</MenuItem>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have also changed the style like this
...
<TextBlock Text="{Binding Path=Content, RelativeSource={RelativeSource Mode=TemplatedParent, AncestorLevel=0}}" />
...
but it is not working. I need to fix this by modifying the style only and cannot change the xaml for MenuItem due to some limitation.
Update 1
I need to make MenuItem text appear on it. The text is there in the TextBlock but its value is coming from the Content Property of MenuItem. MenuItem does not have a Content property instead it has Items Collection that is why the text is not showing. Whereas in the case of Button everything is working the link, text all.
I am working on loose XAML so I have to use Hyperlink for linking. No need to worry about recursion or inheritance as its not an issue. The problem is only that there is no Content property for MenuItem so need to solve this by getting the Item[0].
Update 2
I am also providing the other Resources for reference
<Style x:Key="App_MenuItem" TargetType="{x:Type MenuItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="#FF015DAB"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
<Style.Triggers>
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Margin" Value="0,1,0,1"/>
<Setter Property="Padding" Value="6,3,6,3"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="TopLevelItem">
<Setter Property="Margin" Value="0,1,0,1"/>
<Setter Property="Padding" Value="6,3,6,3"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="SubmenuHeader">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Padding" Value="0,2,0,2"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="SubmenuItem">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Padding" Value="0,2,0,2"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="Menu_LinkForeground" TargetType="{x:Type Hyperlink}">
<Setter Property="Foreground" Value="White" />
<Setter Property="TextDecorations" Value="None" />
</Style>