如果标签内容发生更改,则启用/禁用Combobox

时间:2017-12-12 08:26:33

标签: wpf xaml

如果标签中包含xaml中的内容,是否可以启用/禁用组合框? (我正在寻找 xaml 解决方案。)

<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"/>

1 个答案:

答案 0 :(得分:3)

在纯XAML中,没有。但是,您可以使用IValueConverter将该字符串转换为布尔值:

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}"/>

可能也可能通过Style执行此操作,但说实话会有点奇怪。为了完整起见:

在包含控件/窗口的顶部包含以下命名空间:

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>