Search code examples
c#xamlproperty-binding

Button enable binding to combobox


I need a button disabled untill 3 textboxes are empty and no selection is done on a combobox. I wrote the following XAML code:

<Button x:Name="previewBtn" Content="Anteprima" HorizontalAlignment="Left" Margin="164,33,0,0" VerticalAlignment="Top" Width="75" Height="19" Click="previewBtn_Click_1">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Text.Length, ElementName=titleBox}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Text.Length, ElementName=StEpLabel}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Text.Length, ElementName=seasonBox}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding SelectedIndex, ElementName=cmb}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Binding to the textboes work but the combobox not. What should I write? Thanks


Solution

  • <DataTrigger Binding="{Binding SelectedIndex, ElementName=cmb}" Value="-1">
             <Setter Property="IsEnabled" Value="False" />
      </DataTrigger>
    

    SelectedIndex will be -1 when no item is selected.