2011-11-02 71 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)); 
    } 
    } 

這只是一個想法,所以我真的不知道,如果它在實際工作中... 但我希望它能幫助你一點點

相關問題