0
我使用了一個我在網上找到的示例來創建自定義網格控件。它允許我放置想要製作壓光機的網格線。但是現在,當我去設置背景時,它會停止顯示我的自定義網格線。如何爲customgridcontrol設置自定義背景依賴屬性
我知道這是B/C的OnRender被稱爲第一,然後擺脫我的自定義設置的背景。我打算擺脫OnRender的覆蓋,但仍然沒有運氣。所以我的問題是我怎樣才能讓一個自定義的背景,仍然會允許顯示網格線。
這是我想要在自定義控件上的背景:如果我嘗試將此添加到自定義控件,我的網格線消失。
<Grid.Background>
<RadialGradientBrush>
<GradientStop Color="#FFC3D6F5" Offset="0" />
<GradientStop Color="#FFEFF5FF" Offset="1" />
</RadialGradientBrush>
</Grid.Background>
這是我在網上找到的自定義控件。它只設置customgridline屬性。我現在需要一個自定義背景屬性。這有點難,因爲我的背景不是純色。
namespace Camp_
{
public class GridControl : Grid
{
#region Properties
public bool ShowCustomGridLines
{
get { return (bool)GetValue(ShowCustomGridLinesProperty); }
set { SetValue(ShowCustomGridLinesProperty, value); }
}
public static readonly DependencyProperty ShowCustomGridLinesProperty =
DependencyProperty.Register("ShowCustomGridLines", typeof(bool), typeof(GridControl), new UIPropertyMetadata(false));
public Brush GridLineBrush
{
get { return (Brush)GetValue(GridLineBrushProperty); }
set { SetValue(GridLineBrushProperty, value); }
}
public static readonly DependencyProperty GridLineBrushProperty =
DependencyProperty.Register("GridLineBrush", typeof(Brush), typeof(GridControl), new UIPropertyMetadata(Brushes.Black));
public double GridLineThickness
{
get { return (double)GetValue(GridLineThicknessProperty); }
set { SetValue(GridLineThicknessProperty, value); }
}
public static readonly DependencyProperty GridLineThicknessProperty =
DependencyProperty.Register("GridLineThickness", typeof(double), typeof(GridControl), new UIPropertyMetadata(1.0));
#endregion
protected override void OnRender(DrawingContext dc)
{
if (ShowCustomGridLines)
{
foreach (var rowDefinition in RowDefinitions)
{
dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(0, rowDefinition.Offset), new Point(ActualWidth, rowDefinition.Offset));
}
foreach (var columnDefinition in ColumnDefinitions)
{
dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(columnDefinition.Offset, 0), new Point(columnDefinition.Offset, ActualHeight));
}
dc.DrawRectangle(Brushes.Transparent, new Pen(GridLineBrush, GridLineThickness), new Rect(0, 0, ActualWidth, ActualHeight));
}
base.OnRender(dc);
}
static GridControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GridControl), new FrameworkPropertyMetadata(typeof(GridControl)));
}
}
}
而XAML:
<customgridcontrol:GridControl ShowCustomGridLines="True" GridLineBrush="CornflowerBlue" ShowGridLines="False" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</customgridcontrol:GridControl>
nvm我得到了它的工作。我移動了base.OnRender(dc);在OnRender()中的if語句之上。 – TMan