空集合时以不同方式设置 ComboBox 的样式

Style ComboBox differently when empty collection

我给自己设置了一个 ComboBox 样式,其中包含正常的 Popup。现在,如果集合为空(Count = 0),我想在 Popup 中向用户显示一条消息,而不是当绑定 属性 为 Null 时。该消息应该只是一些 TextBlock,其中包含一些文本。
我试图用 Trigger 更改它,但没有用。 Popup 看起来像这样:

<Popup Name="PART_Popup" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Grid.ColumnSpan="2" Placement="Bottom">
<Border x:Name="DropDownBorder" Height="Auto" MaxHeight="100" Width="100" BorderBrush="Blue" BorderThickness="1">
    <ScrollViewer x:Name="DropDownScrollViewer" Background="White">
        <ContentControl x:Name="PopupContent">
            <Grid RenderOptions.ClearTypeHint="Enabled">
                <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                    <Rectangle x:Name="OpaqueRect" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                </Canvas>
                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" />
            </Grid>
        </ContentControl>
    </ScrollViewer>
</Border></Popup>

使用组合框的项目源并初始化您的 collection。如果您的 collection 不包含任何项目,请使弹出窗口可见等等...

collection.count

您可以利用 ComboBoxHasItems 属性。在这里,我在 Popup 中包含了一个 TextBlock 并根据 HasItems.

控制它的 Visibility
     <Popup
                x:Name="PART_Popup"
                Grid.ColumnSpan="2"
                Margin="1"
                AllowsTransparency="true"
                IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                Placement="Bottom"
                PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                <Popup.Resources>
                    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
                    <local:InverseBooleanToVisiblityConverter x:Key="InverseBooleanToVisiblityConverter" />
                </Popup.Resources>

                <Grid>
                    <TextBlock Text="No Items" Visibility="{Binding HasItems, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBooleanToVisiblityConverter}}" />
                    <Border
                        x:Name="dropDownBorder"
                        Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                        BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"
                        BorderThickness="1"
                        Visibility="{Binding HasItems, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <ScrollViewer x:Name="DropDownScrollViewer">
                            <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                <Canvas
                                    x:Name="canvas"
                                    Width="0"
                                    Height="0"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top">
                                    <Rectangle
                                        x:Name="opaqueRect"
                                        Width="{Binding ActualWidth, ElementName=dropDownBorder}"
                                        Height="{Binding ActualHeight, ElementName=dropDownBorder}"
                                        Fill="{Binding Background, ElementName=dropDownBorder}" />
                                </Canvas>
                                <ItemsPresenter
                                    x:Name="ItemsPresenter"
                                    KeyboardNavigation.DirectionalNavigation="Contained"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Grid>
                        </ScrollViewer>
                    </Border>
                </Grid>
            </Popup>

和转换器,

  public class InverseBooleanToVisiblityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value  ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}