Reputation: 327
I have an Expander WPF control which header's template is a simple TextBlock. I want to hide whole expander if TextBlock.Text (which is filled dynamically from outside) is null or empty.
<Expander>
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"/>
</Expander.Header>
</Expander>
Upvotes: 1
Views: 2647
Reputation: 45
Use binding to Name property to expander visibility with own converter
<Expander Visibility="{Binding Path=Name, Converter={StaticResource EmptyStringToVisibility}}">
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"/>
</Expander.Header>
</Expander>
Converter for example:
class EmptyStringToVisibility : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return String.IsNullOrEmpty(value as string) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 0
Reputation: 8644
You can take reference from this example. Hide Expander ToggleButton if no child items in WPF
Xaml
<ListBox x:Name="lstbx">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander x:Name="exp">
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</Expander.Header>
<Expander.Style>
<Style TargetType="Expander">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Name}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
c#
public Window1()
{
InitializeComponent();
List<HeaderList> lst = new List<HeaderList>();
lst.Add(new HeaderList(){Name= "Header1"});
lst.Add(new HeaderList() { Name = "Header2" });
lst.Add(new HeaderList() { });
lst.Add(new HeaderList() { Name = "Header4" });
lst.Add(new HeaderList() { });
lst.Add(new HeaderList() { });
lst.Add(new HeaderList() { Name = "Header7" });
this.DataContext = this;
lstbx.ItemsSource = lst;
}
}
public class HeaderList
{
public string Name { get; set; }
}
Result
Upvotes: 3