【问题标题】:How can I edit the Menu Item icon when I add the item via Item Source?通过项目源添加项目时如何编辑菜单项目图标?
【发布时间】:2021-10-06 11:37:28
【问题描述】:

我有一个 MenuItem,在这个 MenuItem 中我添加了一个 ItemSource,以便这个 menuItem 的项目是从一个 Observable 集合中创建的。我的 MenuItem 看起来像这样:

 <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>

现在我想编辑使用 ItemSource 创建的项目的 MenuItem.Icon。

我尝试的是这样的:

<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>

但是这确实有效。那么我怎样才能让它发挥作用呢?也许使用 ControlTemplate ?

【问题讨论】:

    标签: wpf xaml menuitem


    【解决方案1】:

    类似下面的东西会起作用

     <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>
    

    【讨论】:

    • 这很有效,但是当我想单击超过 1 个 RadioButton 时,我无法单击另一个 RadioButton。我添加了组名称,但无法更改选定的单选按钮
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多