如果只满足一个条件,MultiDataTrigger将触发

时间:2012-12-30 14:22:05

标签: .net wpf mvvm datatrigger multidatatrigger

在我的UserControl中我有三个ComboBox,必须在启用保存按钮之前选择它们。我为我的ViewModel属性定义了一个带有Bindings的MultiDataTrigger,它们最初设置为null。因此,当加载控件时,按钮会按预期禁用,但只要选择其中一个组合框,就会启用该按钮。据我所知,必须满足MultiDataTrigger的所有条件才能触发它?

这是我的按钮样式:

        <Style x:Key="saveButton" TargetType="{x:Type Button}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=CurrentSpeech.Room, Converter={StaticResource nullToBoolConverter}}" Value="true" />
                    <Condition Binding="{Binding Path=CurrentSpeech.Speaker, Converter={StaticResource nullToBoolConverter}}" Value="true" />
                    <Condition Binding="{Binding Path=CurrentSpeech.Track, Converter={StaticResource nullToBoolConverter}}" Value="true" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Button.IsEnabled" Value="False" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

我的组合框:

            <ComboBox Margin="3" Grid.Row="4" Grid.Column="1" ItemsSource="{Binding Path=Rooms, Mode=OneWay}"
                      SelectedItem="{Binding Path=CurrentSpeech.Room, Mode=TwoWay}" DisplayMemberPath="Name"/>
            <ComboBox Margin="3" Grid.Row="5" Grid.Column="1" ItemsSource="{Binding Path=Tracks, Mode=OneWay}"
                      SelectedItem="{Binding Path=CurrentSpeech.Track, Mode=TwoWay}" DisplayMemberPath="Title"/>
            <ComboBox Margin="3" Grid.Row="6" Grid.Column="1" ItemsSource="{Binding Path=Speakers, Mode=OneWay}" 
                      SelectedItem="{Binding Path=CurrentSpeech.Speaker, Mode=TwoWay}" DisplayMemberPath="Name"/>

我的按钮:

                <Button Style="{StaticResource saveButton}" Margin="3" Grid.Row="9" Grid.Column="1" Command="{Binding Path=CurrentSpeech.SaveCommand}" Width="150" HorizontalAlignment="Right" Content="Speichern"/>

我的转换器:

    [ValueConversion(typeof(object), typeof(bool))]
public class NullToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool result = value == null ? true : false;
        if (parameter != null)
            return !result;
        return result;
    }

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

谢谢, Paxx

1 个答案:

答案 0 :(得分:0)

触发器似乎工作正常,只是你的逻辑不正确。

现在,当RoomSpeakerTrack属性都为空时,触发器将被激活。当程序启动时会发生这种情况,因为ComboBoxes中没有选择任何内容。

因此满足所有3个条件,并且触发器禁用Button

当您在Rooms组合框中选择某些内容时,触发器中的第一个条件不再符合,因为ValueConverter现在为该绑定返回false,因此触发器是禁用,这会导致按钮被启用。

因此,要解决此问题,您应该最初禁用Button,并且仅在转换器为所有绑定返回false(当3个属性为非null时)时才在触发器中设置它。

<Style x:Key="saveButton" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="False"/>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=Room, Converter={StaticResource nullToBoolConverter}}" Value="false" />
                <Condition Binding="{Binding Path=Speaker, Converter={StaticResource nullToBoolConverter}}" Value="false" />
                <Condition Binding="{Binding Path=Track, Converter={StaticResource nullToBoolConverter}}" Value="false" />
            </MultiDataTrigger.Conditions>
            <Setter Property="Button.IsEnabled" Value="True" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>