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
C# 如何在TreeView的某个项目不显示时显示默认文本';没有子项_C#_Wpf_Xaml_Treeview - Fatal编程技术网

C# 如何在TreeView的某个项目不显示时显示默认文本';没有子项

C# 如何在TreeView的某个项目不显示时显示默认文本';没有子项,c#,wpf,xaml,treeview,C#,Wpf,Xaml,Treeview,我有一个像乐队唱片那样的层次结构,在树上展示。当乐队没有任何记录时,我想在层次结构中显示一条类似“无记录”的消息。 我正在尝试使用TargetNullValue,但它不起作用。波段有一个ObservableCollection,如果它为null或其中有null值,则不显示TargetNullValue 以下是我正在使用的XAML: <TreeView ItemsSource="{Binding Bands, TargetNullValue='No bands'}" > <

我有一个像乐队唱片那样的层次结构,在树上展示。当乐队没有任何记录时,我想在层次结构中显示一条类似“无记录”的消息。 我正在尝试使用TargetNullValue,但它不起作用。波段有一个ObservableCollection,如果它为null或其中有null值,则不显示TargetNullValue

以下是我正在使用的XAML:

<TreeView ItemsSource="{Binding Bands, TargetNullValue='No bands'}" >
    <TreeView.ItemContainerStyle>
        <!-- 
        This Style binds a TreeViewItem to a TreeViewItemViewModel. 
        -->
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>
        <HierarchicalDataTemplate
            DataType="{x:Type ViewModels:BandViewModel}" 
            ItemsSource="{Binding Children, TargetNullValue='No bands'}"
        >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding BandName}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate 
            DataType="{x:Type ViewModels:RecordViewModel}" 
            ItemsSource="{Binding Children, TargetNullValue='No records'}"
        >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding RecordName}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

根据问题尝试使用样式解决方案