Question

I have a form that has multiple fields. I have also a "Validate" button that will action a DB input. I would like that button to be activated only if the bare minimum fields are defined by the user.

So far it was quite simple as all the fields were text:

<Button x:Name="Manage" Content="Manage">
    <Button.IsEnabled>
        <MultiBinding Mode="OneWay" Converter="{StaticResource FieldsFilledinToVisible}">
            <Binding ElementName="name1" Path="Text"/>
            <Binding ElementName="name2" Path="Text"/>
        </MultiBinding>
    </Button.IsEnabled>
</Button>

Converter being:

public class AllValuesDefinedConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool isEnabled = false;
        for (int i = 0; i < values.Length; i++)
        {
            isEnabled = isEnabled || string.IsNullOrEmpty(values[i].ToString());
        }
        return !isEnabled;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

But would now have to consider extra check boxes, where the condition should be [Any of these checkboxes checked + previous text fields defined --> Activate the validation button]:

<WrapPanel Style="{StaticResource WrapStyle_Inputs}">
    <CheckBox Content="Check1" IsChecked="{Binding Checked1, Mode=TwoWay}"/>
    <CheckBox Content="Check2" IsChecked="{Binding Checked2, Mode=TwoWay}"/>
    <CheckBox Content="Check3" IsChecked="{Binding Checked3, Mode=TwoWay}"/>
</WrapPanel>

Would you know how I could do so?

thank you!

Was it helpful?

Solution

Converters are the wrong tool for the job. You should instead look at validation, commands, and view models. Your view model would implement validation logic and expose a command that your button is bound to. The command would only be executable if your validation logic passes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top