0

私の UserControl には、保存ボタンを有効にする前にすべて選択する必要がある 3 つの ComboBoxes があります。最初に null に設定されている ViewModel プロパティへのバインディングを使用して MultiDataTrigger を定義しました。したがって、コントロールが読み込まれると、ボタンは期待どおりに無効になりますが、ComboBoxes の 1 つが選択されるとすぐにボタンが有効になります。私が理解したように、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;
    }
}

ありがとう、パックス

4

1 に答える 1

0

トリガーは正常に機能しているようです。正しくないのはあなたのロジックだけです。

現在は機能しているため、 、RoomSpeakerおよびTrackプロパティがすべて null の場合にトリガーがアクティブになります。のいずれかで何も選択されていないため、プログラムの開始時に発生しますComboBoxes

したがって、3 つの条件がすべて満たされ、トリガーによって が無効になりButtonます。

Roomsその後、コンボボックスで何かを選択すると、トリガーの最初の条件が満たされなくなります。これは、そのバインドに対してValueConverternow が返さ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>
于 2012-12-30T22:33:31.073 に答える