Search code examples
c#validationxamlstylesmultibinding

Modifying style and multibinding conditionally


I have a simple XAML that looks as below:

<Window.Resources>
    <Converters:ButtonVisibilityConverter x:Key="m_ButtonConverter"/>
</Window.Resources>

<Grid>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="71,18,0,0" Name="comboBox1" VerticalAlignment="Top" Width="229" SelectionChanged="comboBox1_SelectionChanged">              
    </ComboBox>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,76,0,0" Name="textBox1" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,125,0,0" Name="textBox2" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/>        
    <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="225,175,0,0" Name="button1" VerticalAlignment="Top" Width="75">
        <Button.IsEnabled>
            <MultiBinding Converter="{StaticResource m_ButtonConverter}">
                <Binding ElementName="textBox1" Path="Text" />
                <Binding ElementName="textBox2" Path="Text" />
                <Binding ElementName="comboBox1" Path="Text" />
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
    <Label Content="Box 1" Height="28" HorizontalAlignment="Left" Margin="12,74,0,0" Name="label1" VerticalAlignment="Top" />
    <Label Content="Box 2" Height="28" HorizontalAlignment="Left" Margin="12,123,0,0" Name="label2" VerticalAlignment="Top" />
</Grid>

textbox1 and textbox2 are both mandatory fields now, and that the button does not enable unless both boxes have text.

I want to do the following: When an even number is selected in the combobox, I want to make textbox2 entry as optional. That means, I remove it from the multibinding (of the button's IsEnabled) and also remove the style. However, when an odd number is chosen, I want them back.

Can someone please help?


Solution

  • I was able to do it by conditionally checking in the xaml.cs file on the selectionchanged event of the combobox.

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selVal = (int)comboBox1.SelectedValue;
    
            if ((selVal % 2) == 0)
            {
                // remove the style
                textBox2.Style = null;
    
                // remove from the button's IsEnabled multibinding                 
                _vfs.NumberValidationFlag = false;
                BindingOperations.ClearBinding(button1, Button.IsEnabledProperty);
                BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton());                
            }
            else
            {
                // add back the style
                Style myStyle = (Style)Application.Current.Resources["requiredFieldValidationStyle"];
                textBox2.Style = myStyle;
    
                // add back to the button's IsEnabled multibinding                
                _vfs.NumberValidationFlag = true;
                BindingOperations.ClearBinding(button1, Button.IsEnabledProperty);
                BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton());
            }
        }