开发者

How to show an default text when an item of the TreeView doesn't have subitems

开发者 https://www.devze.com 2023-02-14 13:11 出处:网络
I have an hierarchy like Band - Record, 开发者_运维百科showing on a TreeView. I would like to show in the hierarchy a message like \'No records\' when the band doesn\'t have any records.

I have an hierarchy like Band - Record, 开发者_运维百科showing on a TreeView. I would like to show in the hierarchy a message like 'No records' when the band doesn't have any records. I'm trying to use the TargetNullValue, but it isn't working. The band has an ObservableCollection, and if it is null or it has an null value inside it doesn't show the TargetNullValue.

Here is the XAML I'm using:

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


Try using the styling solution as per this question

0

精彩评论

暂无评论...
验证码 换一张
取 消