XAML with MultiDataTrigger and NullToBoolConverter

本文关键字:NullToBoolConverter and MultiDataTrigger with XAML | 更新日期: 2023-09-27 18:08:33

我试图得到一个按钮启用,如果所有的文本框有一个值,但它不工作,我不能找出原因。

NullToBoolConverter.cs

public class NullToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        TypeConverter converter = TypeDescriptor.GetConverter(typeof(bool));
        bool nullResult = false;
        if (parameter != null)
            nullResult = (bool)converter.ConvertFrom(parameter);
        if (value == null)
            return nullResult;
        else
            return !nullResult;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

XAML-Ressourcedict .

<ex:NullToBoolConverter x:Key="NullToBoolConverter"/>
<Style x:Key="okButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="False" />
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Hostadress, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}" Value="True" />
                <Condition Binding="{Binding UserBox, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}" Value="True"  />
                <Condition Binding="{Binding PasswordTextBox, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}" Value="True" />
                <Condition Binding="{Binding PortBox, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="IsEnabled" Value="True" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

XAML with MultiDataTrigger and NullToBoolConverter

我将您的转换器更改为此,因为它似乎要简单得多…

public class NullToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return false;
        string currentValue = value.ToString();
        if (string.IsNullOrWhiteSpace(currentValue))
            return false;
        return true;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

那么你的XAML样式应该是这样的…

    <ex:NullToBoolConverter
        x:Key="NullToBoolConverter" />
    <Style
        x:Key="okButtonStyle"
        TargetType="{x:Type Button}">
        <Setter
            Property="IsEnabled"
            Value="False" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition
                        Binding="{Binding ElementName=Hostadress, Path=Text, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}"
                        Value="True" />
                    <Condition
                        Binding="{Binding ElementName=UserBox, Path=Text, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}"
                        Value="True" />
                    <Condition
                        Binding="{Binding ElementName=PasswordTextBox, Path=Text, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}"
                        Value="True" />
                    <Condition
                        Binding="{Binding ElementName=PortBox, Path=Text, Converter={StaticResource NullToBoolConverter}, UpdateSourceTrigger=PropertyChanged}"
                        Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter
                    Property="IsEnabled"
                    Value="True" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

调试工作,输入所有文本框将启用按钮。

请注意,我将Binding语句更改为使用ElementName和Path。

我简化了一点代码:

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

我试过你的代码,我你唯一的问题是你只比较null的值,而不是string.Empty。试试这个

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(bool));
    bool nullResult = false;
    if (parameter != null)
        nullResult = (bool)converter.ConvertFrom(parameter);
    if (value == null || value.ToString() == string.Empty)
        return nullResult;
    else
        return !nullResult;
}
相关文章:
  • 没有找到相关文章