WPF MultiDataTrigger和条件 [英] WPF MultiDataTrigger AND condition

查看:97
本文介绍了WPF MultiDataTrigger和条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当我的两个数据网格都选择了项目时,我才想启用按钮.现在,当两个数据网格都具有选择项时,将启用该功能.有什么想法吗?

I would like to enable a button only when both of my two datagrids have selected items. Right now it is enabled when either of the datagrids have selections. Any ideas?

<Button x:Name="button" Content="Z" Grid.Column="1" Margin="0,240,0,0" VerticalAlignment="Top" FontFamily="Wingdings 3" FontSize="21.333" ToolTip="Set the selected alarm for the selected alarm time">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="IsEnabled" Value="True" />
                            <Setter Property="Opacity" Value="1" />
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding ElementName=alarmProfilesDataGrid, Path=SelectedItem}" Value="{x:Null}"/>
                                        <Condition Binding="{Binding ElementName=alarmFilesDataGrid, Path=SelectedItem}" Value="{x:Null}"/>
                                    </MultiDataTrigger.Conditions>
                                    <MultiDataTrigger.Setters>
                                        <Setter Property="IsEnabled" Value="False" />
                                        <Setter Property="Opacity" Value=".5" />
                                        </MultiDataTrigger.Setters>
                                </MultiDataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>

推荐答案

以下是您的代码发生的情况: 当两个数据网格都没有选择时,不是两个条件都成立,而是仅当两个数据网格都没有选择时才满足条件.

Here is what happens with your code: Instead of both conditions to be true when either of the datagrids have selections, the conditions are only met when both of the datagrid don't have selections.

程序启动时,两个数据网格均为空,因此符合您的条件.现在,如果您在任何一个网格中进行选择,则永远不会满足您的条件,并且IsEnabled的值仍为True,即原始值.

When the program starts, both the datagrids are nulls, so your condition is met. Now if you make selection in either of your grids, your condition is never met and the value of IsEnabled remains True i.e. the original value.

要解决此问题,您需要一个转换器:

To correct this problem, you need a converter:

public class NotNullToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool result = value == null ? false : true;

        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

<Style TargetType="Button">
    <Setter Property="IsEnabled" Value="False" />
    <Setter Property="Opacity" Value=".5" />
    <Style.Triggers>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding ElementName=alarmProfilesDataGrid, Path=SelectedItem, Converter={StaticResource NotNullToBoolConverter}}" Value="True"/>
        <Condition Binding="{Binding ElementName=alarmFilesDataGrid, Path=SelectedItem, Converter={StaticResource NotNullToBoolConverter}}" Value="True"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
        <Setter Property="IsEnabled" Value="True" />
        <Setter Property="Opacity" Value="1" />
        </MultiDataTrigger.Setters>
    </MultiDataTrigger>
    </Style.Triggers>
</Style>

这篇关于WPF MultiDataTrigger和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆