【发布时间】:2011-08-01 12:02:27
【问题描述】:
我正在使用来自 codeplex 的 WPF 数据网格。我正在使用 DatagridTemplateColumn,并且我已经编写了数据模板来显示每列中的内容。
现在我必须在数据网格中的任何控件被聚焦时向用户显示一些帮助消息。 为此,我想到了使用装饰层。我使用了 ComboBox 加载事件并访问了它的 adrorner 层。然后我添加了我自己的装饰层,其中显示了一些类似于工具提示的东西。下面是代码。
TextBox txtBox = (TextBox)comboBox.Template.FindName("PART_EditableTextBox", comboBox);
if (txtBox == null)
return;
txtBox.ToolTip = comboBox.ToolTip;
AdornerLayer myAdornerLayer = AdornerLayer.GetAdornerLayer(txtBox);
Binding bind = new Binding("IsKeyboardFocused");
bind.Converter = new KeyToVisibilityConverter();
bind.Source = txtBox;
bind.Mode = BindingMode.OneWay;
PEAdornerControl adorner = new PEAdornerControl(txtBox);
adorner.SetBinding(PEAdornerControl.VisibilityProperty, bind);
PEAdorner 层是这个 ::
public class PEAdornerControl : Adorner
{
Rect rect;
// base class constructor.
public PEAdornerControl(UIElement adornedElement)
: base(adornedElement)
{ }
protected override void OnRender(DrawingContext drawingContext)
{
.....
}
}
现在问题如下。我附上了它在数据网格中的外观的屏幕截图。如果数据网格有超过 4 行,一切都很好。下面是截图
如果数据网格的行数较少,则此装饰器进入数据网格内部并且对用户不可见。截图如下
如何在 DataGrid 上方获得这个装饰层?请帮帮我!!!
【问题讨论】:
-
如果我没记错的话,并不是装饰器进入数据网格内部,而是数据网格的大小不够大,无法正确显示装饰器。这是由于父级的 OnMesaure 方法没有考虑到 adnorner 层。您可能需要考虑创建自己的父容器来考虑这一点,或者(更有可能)将装饰器附加到包含父装饰器层。
-
@JMcCarty -- 我明白了。但我很抱歉地说我无法理解你在最后两行中想说什么。您能否发布一些示例代码或者可能是一个链接以帮助我理解?
标签: wpf datagrid z-order adornerlayer