我使用了我在网上找到的一个例子来创建自定义网格控件。它允许我放置我想要制作日历的那种网格线。但现在,当我去设置背景时,它会停止我的自定义网格线显示。
我知道这是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>
答案 0 :(得分:1)
我认为您只需在xaml代码中设置标准背景属性,如下所示:
<customgridcontrol:GridControl ShowCustomGridLines="True" GridLineBrush="CornflowerBlue" ShowGridLines="False" Background="Binding{ StaticResource BrushYouWantTo" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</customgridcontrol:GridControl>
“你想要的画笔”必须在xaml代码中设置为资源,如下所示:
<Window.Resources>
<RadialGradientBrush Name="BrushYouWantTo">
<GradientStop Color="#FFC3D6F5" Offset="0" />
<GradientStop Color="#FFEFF5FF" Offset="1" />
</RadialGradientBrush>
</Window.Resources>
但是如果你想在程序运行时更改背景颜色,你需要一个依赖属性和一个重新加载gui的事件处理程序。要使用此事件处理程序,您必须使用
定义它new UIPropertyMetadata(CustomGridBackgroundChanged)
事件处理程序定义如下所示:
private static void CustomGridBackgroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
WindowName win = (WindowName)sender;
if (win.PropertyChanged != null)
{
// at this place the gui will be reloaded
win.PropertyChanged(win, new PropertyChangedEventArgs(null));
}
}
这只是一个想法,所以我真的不知道它是否在实践中有效... 但我希望它对你有所帮助