2011-11-02 69 views
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> 
+0

nvm我得到了它的工作。我移动了base.OnRender(dc);在OnRender()中的if语句之上。 – TMan

回答

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)); 
    } 
    } 

这只是一个想法,所以我真的不知道,如果它在实际工作中... 但我希望它能帮助你一点点

相关问题