在我的UserControl中我有三个组合框,在启用保存按钮前必须选择所有组合框。我定义了一个MultiDataTrigger绑定到我的ViewModel属性,它们最初设置为null。因此,当控件加载时,按钮被禁用,但一旦选择了其中一个组合框,该按钮将被启用。根据我的理解,MultiDataTrigger的所有条件必须满足才能启动?如果只有一个条件得到满足,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
你试过调试吗?你有没有检查你的转换器中的值是否真的是空的? – Blachshma
嗨Blachshma,我刚刚检查。当窗口加载时,转换器在所有3个条件下都返回true。如果我选择其中一个,转换器将返回错误。所以看起来转换器工作正常。 – Paxx
哦,这不是问题。它仍然无法正常工作,只有转换器是。只要满足其中一个条件,按钮将被启用,这不是我想要的。 – Paxx