Raizzen
Raizzen

Reputation: 192

Enable/Disable Combobox if label content changed

Is it possible to enable/disable the combobox if the label has content in the xaml? (I am looking for a xaml solution.)

<Label x:Name="lbl_AusgewählteEmail" HorizontalAlignment="Left"
    Margin="37,132,0,0" VerticalAlignment="Top" Width="607"
    Content="{Binding ElementName=combx_UnzustellbarMailAuswahl, Path=SelectedItem}"/>

<ComboBox x:Name="combx_Auswahl" HorizontalAlignment="Left" 
    Margin="37,219,0,0" VerticalAlignment="Top" Width="318"/>

Upvotes: 1

Views: 87

Answers (1)

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

In pure XAML, no. You can however use an IValueConverter to turn that string into a boolean:

public class NonEmptyStringToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string)
            return !String.IsNullOrEmpty((string) value);
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}
<Window.Resources>
    <yourNameSpace:NonEmptyStringToBooleanConverter x:Key="StringToBool"/>
</Window.Resources>

<Label x:Name="lbl_AusgewählteEmail" HorizontalAlignment="Left"
   Margin="37,132,0,0" VerticalAlignment="Top" Width="607"
   Content="{Binding ElementName=combx_UnzustellbarMailAuswahl, Path=SelectedItem}"/>

<ComboBox x:Name="combx_Auswahl" HorizontalAlignment="Left" 
    Margin="37,219,0,0" VerticalAlignment="Top" Width="318"
    IsEnabled="{Binding ElementName=combx_UnzustellbarMailAuswahl, Path=SelectedItem, Converter={StaticResource StringToBool}"/>

You could potentially also do this via a Style but that would be a bit weird to be honest. For the sake of completeness:

Include the following namespace at the top of your containing control/window:

xmlns:system="clr-namespace:System;assembly=mscorlib"
<ComboBox.Style>
    <Style TargetType="ComboBox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=combx_UnzustellbarMailAuswahl, Path=SelectedItem}" Value="{x:Static system:String.Empty}">
                <Setter Property="IsEnabled" Value="False"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=combx_UnzustellbarMailAuswahl, Path=SelectedItem}" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox.Style>

Upvotes: 3

Related Questions