我为此尝试了几种不同的解决方案,但不能满足我的所有需求。
我们有一个可观察的对象集合,每个对象都有一个状态和一个名称。这是一种正在运行的项目的任务列表。为了在WPF中显示此列表,我们有一些代码将每个项目表示为带有某些颜色和动画的椭圆。
问题在于,我们希望在鼠标悬停时或任务处于给定状态时,将项目名称显示为“弹出窗口”。
尝试#1
我的第一次尝试是将它实现为带有实际WPF弹出窗口的Datatemplate(用作ItemTemplate)。我实现了两个数据触发器-一个用于鼠标悬停,另一个用于任务状态。我根据椭圆定位弹出窗口,一切都很好。但是,移动窗口或切换到其他窗口会使弹出窗口位于所有内容的顶部。
尝试#2
我没有使用弹出窗口,而是在画布中使用了文本框。直到在列表框中使用Datatemplate之前,这都很好用。项目宿主(堆栈面板)最终会截断字符串。
这是示例代码:
<DataTemplate x:Key="EllipseTemplate">
<Grid Height="40" Width="40">
<Canvas Name="PopupCanvas" HorizontalAlignment="Center" Width="500">
<TextBlock Name="PopupName"
Width="{Binding ElementName=PopupCanvas, Path=ActualWidth}"
Text="{Binding}"
Background="Transparent"
FontSize="16" HorizontalAlignment="Center" TextAlignment="Center" FontWeight="Bold"
Canvas.Top="-25"
Visibility="Collapsed"
/>
</Canvas>
<Ellipse x:Name="Ellipse" Height="25" Width="25" Margin="0"
HorizontalAlignment="Center" VerticalAlignment="Center"
Fill="Green"
RenderTransformOrigin="0.5, 0.5" StrokeThickness="0.5" Stroke="Black">
</Ellipse>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter TargetName="PopupName" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<Grid Name="Test" Background="LightGoldenrodYellow" ClipToBounds="False" Margin="50">
<ListBox Name="OverlayTest"
Background="CornflowerBlue"
BorderThickness="0"
VerticalAlignment="Center"
Margin="10"
ClipToBounds="False"
ItemTemplate="{StaticResource EllipseTemplate}">
<sys:String>Very long string that will get clipped</sys:String>
<sys:String>Two</sys:String>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True" Margin="10,50,10,50" ClipToBounds="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
尝试#3
我将画布/文本框移到了数据模板之外,并创建了一个网格以将其置于椭圆的列表框上方。从布局的角度来看,这是可行的,但在检查鼠标悬停以及将文本框居中活动/悬停的控件的中心方面会造成很大的麻烦。
这样一来,我就无法实现所需的实现方式。有人有什么建议吗?
好吧,我有另一个主意。我以前有问题ListBox
。尝试更换ListBox
用ItemsControl
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句