wpf 菜单项 style.trigger

wpf menuitem style.trigger

我正在尝试实现一个可检查的菜单,一次只检查一个菜单项,即单选按钮行为。我想通过样式和数据触发器来实现这一点,以将其保留在 xaml 中。目的是观察其他项目;只要有任何其他为真,就应将其设置为假。实际上,一次只检查一个,但是需要按两次按钮。我怎样才能让它一键工作?

<MenuItem Header="Header">
            <MenuItem x:Name="ChangesMenuItem" Header="Changes" IsCheckable="True" Command="{Binding ViewChangesCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Style.Triggers>                                
                            <DataTrigger Binding="{Binding ElementName=SettingsMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=SyncMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
            <MenuItem x:Name="SettingsMenuItem" Header="Settings" IsCheckable="True" Command="{Binding ViewSettingsCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=ChangesMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=SyncMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
            <MenuItem x:Name="SyncMenuItem" Header="Sync" IsCheckable="True" Command="{Binding ViewSyncCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=ChangesMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=SettingsMenuItem, Path=IsChecked}" Value="True">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
        </MenuItem>

我有一个 header,它会根据选择而变化,所以我决定绑定到那个 属性。

<Menu>
        <MenuItem Header="{Binding Header}">
            <MenuItem x:Name="ChangesMenuItem" Header="Changes" IsCheckable="True" Command="{Binding ViewChangesCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Setter Property="IsChecked" Value="False"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Header}" Value="Changes">
                                <Setter Property="IsChecked" Value="True"/>
                            </DataTrigger>                                                             
                        </Style.Triggers>                            
                    </Style>
                </MenuItem.Style>                   
            </MenuItem>
            <MenuItem x:Name="SettingsMenuItem" Header="Settings" IsCheckable="True" Command="{Binding ViewSettingsCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Setter Property="IsChecked" Value="False"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Header}" Value="Settings">
                                <Setter Property="IsChecked" Value="True"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
            <MenuItem x:Name="SyncMenuItem" Header="Sync" IsCheckable="True" Command="{Binding ViewSyncCommand}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Setter Property="IsChecked" Value="False"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Header}" Value="Sync">
                                <Setter Property="IsChecked" Value="True"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
        </MenuItem>
    </Menu>