Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如果标头为空,则隐藏整个扩展器_Wpf_Triggers_Datatrigger_Wpf Style - Fatal编程技术网

Wpf 如果标头为空,则隐藏整个扩展器

Wpf 如果标头为空,则隐藏整个扩展器,wpf,triggers,datatrigger,wpf-style,Wpf,Triggers,Datatrigger,Wpf Style,我有一个扩展WPF控件,它的头模板是一个简单的文本块。如果TextBlock.Text(从外部动态填充)为null或空,我想隐藏整个扩展器 <Expander> <Expander.Header> <TextBlock Text="{Binding Path=Name}"/> </Expander.Header> </Expander> 您可以参考此示例。 Xaml <ListBox x:N

我有一个扩展WPF控件,它的头模板是一个简单的文本块。如果TextBlock.Text(从外部动态填充)为null或空,我想隐藏整个扩展器

<Expander>
    <Expander.Header>
        <TextBlock Text="{Binding Path=Name}"/>
    </Expander.Header>
</Expander>

您可以参考此示例。

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#

公共窗口1()
{
初始化组件();
List lst=新列表();
lst.Add(new HeaderList(){Name=“Header1”});
lst.Add(new HeaderList(){Name=“Header2”});
lst.Add(新的HeaderList(){});
lst.Add(new HeaderList(){Name=“Header4”});
lst.Add(新的HeaderList(){});
lst.Add(新的HeaderList(){});
lst.Add(new HeaderList(){Name=“Header7”});
this.DataContext=this;
lstbx.ItemsSource=lst;
}
}
公营班主任
{
公共字符串名称{get;set;}
}
结果


使用绑定到名称属性来扩展自身转换器的可见性

        <Expander Visibility="{Binding Path=Name, Converter={StaticResource EmptyStringToVisibility}}">
             <Expander.Header>
                 <TextBlock Text="{Binding Path=Name}"/>
             </Expander.Header>
        </Expander>

这很奇怪,但这种直接的解决方案对我不起作用。将样式添加到带有DataTrigger的Extender到TextBox解决了这个问题。
        <Expander Visibility="{Binding Path=Name, Converter={StaticResource EmptyStringToVisibility}}">
             <Expander.Header>
                 <TextBlock Text="{Binding Path=Name}"/>
             </Expander.Header>
        </Expander>
    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();
         }
    }