我给自己设计了一个包含正常Popup
的ComboBox
。现在,我想在集合为空(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>
您可以使用 ComboBox
的 HasItems
属性。在这里,我在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();
}
}
使用组合框的 itemsource 并初始化集合。如果您的集合不包含任何项目,请使弹出窗口可见,依此类推...
集合.计数