基于计数在 WPF ListBox 中使用 DataTrigger 隐藏 ListBox 项中的按钮

Hide Button in ListBox Item using DataTrigger in WPF ListBox Based on Count

我有一个 ListBox,因为 ItemTemplate 我有一个 TextBlock 和一个 Delete Button

我的需求:如果ObservableCollection<string> Person只有一条记录,那么我需要隐藏删除Button。如果有不止一条记录,那么我需要为所有项目显示删除 Button

XAML:

<ListBox ItemsSource="{Binding Person, UpdateSourceTrigger=PropertyChanged}" Background="Transparent"  Margin="0 10" HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBox Grid.Row="0" Text="{Binding Contact, UpdateSourceTrigger=PropertyChanged}" />
                    <Button  Grid.Row="1" Content="X" Foreground="Red" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}"  Value="1">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

数据触发器:

    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}"  Value="1">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>

请帮助我如何根据我的要求设置 DataTrigger

DataTrigger 可以采用按钮的样式:

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Items.Count,
                                 RelativeSource={RelativeSource AncestorType=ListBox}}"
                             Value="1">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>