将 MenuItem 的 #items 绑定到 CompositeCollection 中的可见性

Binding #items of a MenuItem to visibility in a CompositeCollection

我有一个 MenuItem,它通过 ObservableCollection 加上一个静态 MenuItem 填充,我只想在集合为空时显示它。

为此,我正在使用 CompositeCollection,并尝试通过 DataTrigger 触发 MenuItem 的可见性,但无济于事。

无论我使用 HasItems 还是 Items.Count,绑定本身似乎都是问题所在:

<MenuItem x:Name="ClientsList" Header="Clients">
    <MenuItem.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding Source={x:Static win32:ClientProcessHandler.ClientProcessList}}" />
            <MenuItem Header="No Client.." IsEnabled="False">
                <MenuItem.Style>
                    <Style TargetType="MenuItem">
                        <Style.Triggers>
                            <!-- Here is the binding -->
                            <DataTrigger Binding="{Binding Items.Count, ElementName=ClientsList}" Value="2">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
        </CompositeCollection>
    </MenuItem.ItemsSource>
    <MenuItem.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="IsChecked" Value="{Binding IsSelected, Mode=OneWay}" />
            <Setter Property="Header" Value="{Binding Path}" />
            <Setter Property="Command" Value="{Binding DataContext.SelectClient, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
            <Setter Property="CommandParameter" Value="{Binding }" />
            <Setter Property="StaysOpenOnClick" Value="True" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

绑定到源集合的 Count 属性:

<DataTrigger Binding="{Binding Count, 
    Source={x:Static win32:ClientProcessHandler.ClientProcessList}}" Value="0">
    <Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>