本文介绍了在 WPF 中应用条件样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,需要在菜单项上应用条件样式,这是我的代码片段:

I ran through one issue, need to apply conditional styling on Menu Item, here is a bit from my Code-Snippet:

<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,10,0,0">
    <Menu HorizontalAlignment="Left" KeyboardNavigation.TabNavigation="Once" Background="Transparent" d:LayoutOverrides="Height">
        <MenuItem Header="Menu1" Style="{DynamicResource M_Left}"  />
        <MenuItem Header="Menu2" Style="{DynamicResource M_Middle}" />
        <MenuItem Header="Menu3" Style="{DynamicResource M_Right}" Visibility="{Binding IsEligibleToDisplay, Converter={StaticResource MyVisibilityConverter}}" />
    </Menu>
</Grid>

在上面,IsEligibleToDisplay 是一个 bool 属性,MyVisibilityConverter 根据 True 或 false 将 Visibility 设置为 Visible 或 Hidden.

In above, IsEligibleToDisplay a bool property and MyVisibilityConverter sets Visibility either to Visible or Hidden on the basis of True or false.

预期是什么?

如果Menu3"的可见性是隐藏的,即 IsEligibleToDisplay = false 那么Menu2"的样式应该是 Style="{DynamicResource M_Right}" 否则 Style="{DynamicResource M_Middle}"

If Visibility of "Menu3" is Hidden i.e. IsEligibleToDisplay = false then Style of "Menu2" should be Style="{DynamicResource M_Right}" otherwise Style="{DynamicResource M_Middle}"

类似(这只是假设,请不要检查语法 - 它是错误的:)):

Something like (its just hypothetic, please do not check syntax - its wrong:)):

<MenuItem Header="Menu2" Style="IsEligibleToDisplay ? {DynamicResource M_Middle} : {DynamicResource M_Right}" />

任何帮助将不胜感激!

推荐答案

如果您的要求只是使用 XAML,我想您可以使用 DataTriggers.

If your requirement is to use just XAML, I guess you can use DataTriggers.

您不能直接在 Style 属性中设置条件",但必须将其移动到 Style 声明中.

You cannot set your "condition" directly in the Style property, but you have to move it inside the Style declaration.

可能这个小样本可以帮助您解决您的任务:

Probably this small sample can help you in resolving your task:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="400">

    <Window.Resources>

        <Style x:Key="ConditionalStyle" TargetType="MenuItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Menu3, Path=Visibility}" Value="Visible">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=Menu3, Path=Visibility}" Value="Hidden">
                    <Setter Property="Foreground" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>


    <StackPanel>
        <Menu HorizontalAlignment="Left" KeyboardNavigation.TabNavigation="Once" Background="Transparent">
            <MenuItem Header="Menu1" />
            <MenuItem Header="Menu2" Style="{DynamicResource ConditionalStyle}" />
            <MenuItem Name="Menu3" Header="Menu3" Visibility="Visible" />
        </Menu>

        <Button Content="ClickMe" Margin="10" Click="Button_Click" />
    </StackPanel>
</Window>

我使用按钮只是将 Menu3 从可见切换到隐藏,反之亦然.我使用了一个简单的处理程序:

I used the button just to switch Menu3 from Visible to Hidden and viceversa. I used a simple handler:

private void Button_Click(object sender, RoutedEventArgs e)
{
    if(Menu3.Visibility == System.Windows.Visibility.Visible)
    {
        Menu3.Visibility = System.Windows.Visibility.Hidden;
        return;
    }
    Menu3.Visibility = System.Windows.Visibility.Visible;
}

我希望这个解决方案适合你.

I hope this solution is suitable for you.

这篇关于在 WPF 中应用条件样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:50