Folder pane visibility independent of shortcut contents

I have a stack panel that I want to make visible based on the contents of the shortcuts. Just not sure why it is not working for me. What is in bold is what I want to hide. Any suggestion?

<StackPanel Orientation="Horizontal">
<Label Nane="lblCarrier" Content="{Binding Path=Carrier}" />
**<StackPanel Orientation="Horizontal">
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Setter Property="Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Content, ElementName=lblCarrier}" Value="">
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <Label x:Name="lblCarrierGrade" Content="Grade Carrier:" />
    <TextBox x:Name="txtCarrierGrade1" />
    <TextBox x:Name="txtCarrierGrade2" />
</StackPanel>**

+5
source share
2 answers

Maybe Contentit is null, not String.Empty.

You can try using TargetNullValue

<DataTrigger Binding="{Binding Content, ElementName=lblCarrier,TargetNullValue=''}" Value="">
      <Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
+8
source

Why not use a converter? Add a class project to you:

class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return string.IsNullOrEmpty(value as string) ? Visibility.Hidden : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In the definition, Windowadd the following:

xmlns:myNamespace="clr-namespace:[YourProjectName]"

Then somewhere in the resources add this

<myNamespace:VisibilityConverter x:Key="myConverter"/>

Now you can use it:

 <Style TargetType="StackPanel">
        <Setter Property="Visibility" 
                Value="{Binding Content, ElementName=lblCarrier,
                                Converter = {StaticResources myConverter}}"/>
+1
source

All Articles