【发布时间】:2014-06-15 12:41:44
【问题描述】:
我有一个UserControl,它在 XAML 中的引用如下:
<local:ColumnGraphRenderCtrl x:Name="graphCtrl" Grid.Column="1"
Height="Auto" Width="Auto"/>
有问题的UserControl 有几个矩形,它们显示得很好。
但是,如果我指定Background 颜色,则指定的颜色会遮挡矩形,并且只显示颜色。例如:
<local:ColumnGraphRenderCtrl x:Name="graphCtrl" Background="Blue" Grid.Column="1"
Height="Auto" Width="Auto"/>
(如果我将颜色更改为“透明”,矩形就会变得可见。)
我还尝试将ControlTemplate 用于UserControl(作为Style 的一部分),但我得到了相同的结果(即背景颜色阻挡了UserControl 的内容)。
我在MSDN 上查找了 Control.Background 属性,它提供了以下备注:
Background 属性仅适用于 控制。控件的默认样式指定其外观 当控件的状态发生变化时。例如,如果您设置 按钮上的背景属性,按钮只有在 它没有被按下或禁用。如果你想创建一个控件 有更高级的背景自定义,你必须定义 控件的样式。
此属性仅影响其模板使用 背景属性作为参数。在其他控件上,此属性 没有影响。
MSDN中的注释有什么意义,如何指定背景颜色而不遮挡控件内容?
编辑:内容控件(矩形)是在代码隐藏中手动添加的,如果有影响的话。
用户控制代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace GraphingWithShapes
{
public partial class ColumnGraphRenderCtrl: UserControl
{
private ObservableCollection<NameValuePair> _dataPoints = null;
private List<Color> _columnColors = new List<Color>() { Colors.Blue, Colors.Red, Colors.Green };
public ColumnGraphRenderCtrl()
{
InitializeComponent();
}
public void SetData(ObservableCollection<NameValuePair> data)
{
_dataPoints = data;
_dataPoints.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_dataPoints_CollectionChanged);
InvalidateVisual();
}
void _dataPoints_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
InvalidateVisual();
}
public double GetLargestValue()
{
double value = 0;
foreach (NameValuePair nvp in _dataPoints)
{
value = Math.Max(value, nvp.Value);
}
return value;
}
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
}
protected override void OnRender(DrawingContext drawingContext)
{
if (_dataPoints != null)
{
double spaceToUseY = ActualHeight * 0.8;
double spaceToUseX = ActualWidth * 0.8;
double barWidth = spaceToUseX / _dataPoints.Count;
double largestValue = GetLargestValue();
double unitHeight = spaceToUseY / largestValue;
double bottom = ActualHeight * 0.9;
double left = ActualWidth * 0.1;
Brush fillBrush;
Pen outlinePen = new Pen(Brushes.Black, 1);
int nIndex = 0;
Rect rect;
double height;
foreach (NameValuePair nvp in _dataPoints)
{
fillBrush = new SolidColorBrush(_columnColors[nIndex % _columnColors.Count]);
height = (nvp.Value * unitHeight);
rect = new Rect(left, bottom - height, barWidth, height);
drawingContext.DrawRectangle(fillBrush, outlinePen, rect);
left += rect.Width;
nIndex++;
}
}
}
}
}
【问题讨论】:
-
您可以发布您的
UserControl代码吗?我尝试按照您的描述自己创建一个,但无法复制问题 -
是的,我在同一条船上。我在几个项目中多次这样做,并且内容始终可见,因为 Usercontrol 的背景始终位于其子项的底部。
-
刚刚看到您的编辑。是的,这可能会有所作为。如果您正在绘制到 UserControl。我的建议是在你的用户控件上放置一个画布并在上面绘制。
-
@TzahMama 添加了控件的代码。
-
@RobertSnyder 会试试的。