Home > Software design >  How can I edit the Menu Item icon when I add the item via Item Source?
How can I edit the Menu Item icon when I add the item via Item Source?

Time:10-06

I have an MenuItem and in this MenuItem I add an ItemSource, so that the Items of this menuItem are createt from an Observable Collection. My MenuItem look like that:

 <MenuItem Foreground="Black" 
                          FontFamily="{Binding ElementName=wpfAudit, Path=FontFamily}"
                          FontSize="{Binding ElementName=wpfAudit, Path=FontSize}" 
                          FontWeight="{Binding ElementName=wpfAudit, Path=FontWeight}" 
                          Header="Artikellabel Drucker" 
                          ItemsSource="{Binding ocArtikellabeldrucker, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

                
                </MenuItem>

Now I want to edit the MenuItem.Icon of the Items which I created with an ItemSource.

What I tried is this:

<MenuItem.Resources>
                        <RadioButton x:Key="RadioButtonResource" x:Shared="false" HorizontalAlignment="Center"
                 GroupName="MenuItemRadio" IsHitTestVisible="False" IsChecked="{Binding IstDrucker}" Style="{StaticResource {x:Type RadioButton}}"/>
                    </MenuItem.Resources>

But this dosent work. So how can I get that to work? Maybe with an ControlTemplate ?

CodePudding user response:

Something like below will work

 <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <StackPanel Orientation="Horizontal">
                        <RadioButton IsChecked="True" Content="Test Item" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
  • Related