如何强制wpf文本框的内容在其他控件上方类似于Excel单元的其他控件上方显示
我有一个ItemScontrol,其中包含每个绑定项目的文本框,如果其内容宽,则允许叠加文本框内容,而不是类似于Excel单元格覆盖行为的文本框宽度。 有办法这样做吗?
<ItemsControl ItemsSource="{Binding Path=MyCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Width="100"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}" TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用工具提示。您只需要自己绑定源即可。
有关更多信息:
You can use tooltip for that. You just need to bind the source with itself.
For further information:
DrawToolTip Event
Set ToolTipSize
要复制类似的行为,您可以使用
弹出式
。要实现类似的行为,您首先必须禁用
textbox
的内容包装。然后用
textblock
替换当前textbox
,该>用于显示文本。弹出
实际上包含Editabletextbox
,然后将textblock
叠加在确切位置,从而隐藏textblock 使其似乎伸展和覆盖相邻的项目。
MultiTrigger
将在焦点移动ListBoxItem
之外后立即关闭弹出窗口。为了提高性能,您应该将
virtualizingstackpanel
用作项目。托而言,
这只是一个原始的例子,是概念证明。您必须改善行为。例如,当用户滚动或移动
window
时,您需要关闭弹出
。否则,由于弹出
本身是窗口
,因此popup
不会移动以遵循放置目标。您可以将相关逻辑移至附件的行为。您可能还想改善选择行为。当前,选择突出显示边框(实际上)并未扩展到围绕弹出窗口。您必须通过在
textbox
上应用边框来模仿此功能,该边框将复制ListBoxItem
突出显示边框。To replicate a similar behavior, you can make use of a
Popup
.To implement a similar behavior, you first must disable content wrapping of your
TextBox
.Then replace the current
TextBox
with aTextBlock
, which is used to display the text.The
Popup
, which actually contains the editableTextBox
, will then overlay thisTextBlock
at the exact position, thus hiding theTextBlock
to make it appear to stretch and overlay the adjacent items.A
MultiTrigger
will close the Popup as soon as the focus moved outside theListBoxItem
.To improve performance you should use the
VirtualizingStackPanel
as items host.To-do
This is just a raw example, a proof of concept. You would have to improve the behavior. For example, you would want to close the
Popup
when the user scrolls or moves theWindow
. Otherwise, sincePopup
itself is aWindow
, thePopup
would not move to follow the placement target. You could move the related logic to an attached behavior.You likely also want to improve the selection behavior. Currently the selection highlight border does not (virtually) extend to surround the Popup. You have to mimic this by applying a Border on the
TextBox
that will replicate theListBoxItem
highlight border.我通过使用此助手依赖属性 https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-count-properties/ 作为itemscontrol的容器模板项目索引和绑定网格。Zindex与相邻文本框上方显示的反向索引。
I managed to produce the excel cells overlay behavior by using a Grid with dynamic column count using this helper dependency properties https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/ as a container template of ItemsControl and binding the column index of each textbox to the ordered item index and binding Grid.ZIndex to the reversed index to be displayed above the adjacent text boxes.