[英]Adorner, cannot set opacity, border, background wpf
我有一個自定義的裝飾器。 (在拖動UIelement時顯示圖像)
現在,此裝飾器是透明的,但我希望它不透明。 我也不知道如何設置背景和其他看起來不錯的東西,例如邊框。
這是我的自定義裝飾器:
/// <summary>
/// Adorner, to show a picture of the listbox-item we are dragging.
/// </summary>
public class DraggedAdorner : Adorner
{
private readonly ContentPresenter draggedItemPresenter;
private readonly AdornerLayer draggedItemAdornerLayer;
private double left;
private double top;
/// <summary>
/// Initializes a new adorner, which will display the dragged listbox-item.
/// </summary>
/// <param name="listBoxItem">ListBoxItem, which we want to show while dragging.</param>
/// <param name="listBox">ListBoxItem, which we want to show while dragging.</param>
/// <param name="adornerLayer">Presentation layer for the adorner.</param>
/// <param name="width"></param>
public DraggedAdorner(ListBoxItem listBoxItem, ListBox listBox, AdornerLayer adornerLayer, double width, double height)
: base(listBox)
{
draggedItemAdornerLayer = adornerLayer;
draggedItemPresenter = new ContentPresenter
{
Content = listBoxItem,
Width = width,
Height = height
};
draggedItemAdornerLayer.Add(this);
}
/// <summary>
/// Sets the position of the dragged adorner.
/// </summary>
/// <param name="newLeft">new left position of the adorner</param>
/// <param name="newTop">new top position of the adorner</param>
public void SetPosition(double newLeft, double newTop)
{
// -1 and +13 align the dragged adorner with the dashed rectangle that shows up
// near the mouse cursor when dragging.
left = newLeft;
top = newTop;
if (draggedItemAdornerLayer != null)
{
draggedItemAdornerLayer.Update(AdornedElement);
}
}
protected override Size MeasureOverride(Size constraint)
{
draggedItemPresenter.Measure(constraint);
return draggedItemPresenter.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
draggedItemPresenter.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
return draggedItemPresenter;
}
protected override int VisualChildrenCount
{
get { return 1; }
}
public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
{
GeneralTransformGroup result = new GeneralTransformGroup();
result.Children.Add(base.GetDesiredTransform(transform));
result.Children.Add(new TranslateTransform(left, top));
return result;
}
/// <summary>
/// Removes the this adorner form the adornerlayer.
/// </summary>
public void Detach()
{
draggedItemAdornerLayer.Remove(this);
}
}
誰能幫我?
我現在以一種簡單的方式添加了背景:我剛剛添加了一個矩形,其屬性為fill = backgroundcolor:
public class DraggedAdorner : Adorner
{
private readonly Rectangle background;
private readonly ContentPresenter draggedItemPresenter;
private readonly AdornerLayer draggedItemAdornerLayer;
private double left;
private double top;
/// <summary>
/// Initializes a new adorner, which will display the dragged listbox-item.
/// </summary>
/// <param name="listBoxItem">ListBoxItem, which we want to show while dragging.</param>
/// <param name="listBox">ListBoxItem, which we want to show while dragging.</param>
/// <param name="adornerLayer">Presentation layer for the adorner.</param>
/// <param name="width"></param>
public DraggedAdorner(ListBoxItem listBoxItem, ListBox listBox, AdornerLayer adornerLayer, double width, double height)
: base(listBox)
{
draggedItemAdornerLayer = adornerLayer;
draggedItemPresenter = new ContentPresenter
{
Content = listBoxItem,
Width = width,
Height = height
};
Rectangle rectangle = new Rectangle
{
Width = width,
Height = height,
Fill = new SolidColorBrush(Colors.SkyBlue)
};
background = rectangle;
draggedItemAdornerLayer.Add(this);
}
/// <summary>
/// Sets the position of the dragged adorner.
/// </summary>
/// <param name="newLeft">new left position of the adorner</param>
/// <param name="newTop">new top position of the adorner</param>
public void SetPosition(double newLeft, double newTop)
{
//only set the left property the first time, to not allow dragging the listboxitem
//out of the listbox in the horizontal direction.
if (Math.Abs(left) < 0.1)
{
left = newLeft + 20;
}
top = newTop;
if (draggedItemAdornerLayer != null)
{
draggedItemAdornerLayer.Update(AdornedElement);
}
}
protected override Size MeasureOverride(Size constraint)
{
draggedItemPresenter.Measure(constraint);
background.Measure(constraint);
return draggedItemPresenter.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
draggedItemPresenter.Arrange(new Rect(finalSize));
background.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
return index == 0 ? (Visual) background : draggedItemPresenter;
}
protected override int VisualChildrenCount
{
get { return 2; }
}
public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
{
GeneralTransformGroup result = new GeneralTransformGroup();
result.Children.Add(base.GetDesiredTransform(transform));
result.Children.Add(new TranslateTransform(left, top));
return result;
}
/// <summary>
/// Removes the this adorner form the adornerlayer.
/// </summary>
public void Detach()
{
draggedItemAdornerLayer.Remove(this);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.