wpf menuitem style.trigger

时间:2017-06-12 19:46:23

标签: wpf menuitem datatrigger

我正在尝试实现一个可检查菜单,其中一次只检查一个菜单项,即单选按钮行为。我想用样式和数据触发器实现这一点,以保持它在xaml中。目的是观察其他项目;只要其他任何一个为真,就应该设置为false。实际上,一次只检查一个,但是需要按下两个按钮。如何只需按一下按钮即可使其工作?

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

1 个答案:

答案 0 :(得分:0)

我有一个根据选择而改变的标题,所以我决定绑定到该属性。

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