Стиль GridLines на ItemsControl WPF - PullRequest
       14

Стиль GridLines на ItemsControl WPF

0 голосов
/ 02 ноября 2011

На данный момент в моем календаре граница размещена только вокруг текстовых блоков (дат), которые необходимы для этого месяца. У меня есть сетка, которая имеет 7 столбцов и 6 строк, так что это 42 ячейки. В месяце есть только максимум 31 день (ячейки), поэтому у меня много пустых ячеек, которые не имеют границы вокруг него. Как я могу изменить это так, чтобы все 42 ячейки имели рамку вокруг, чтобы он выглядел так, как должен выглядеть каландр. Заранее спасибо. :)

<Grid Name="controlGrid" Margin="0,56,0,0">
  <Grid.ColumnDefinitions >
    <ColumnDefinition Width="86*" />
    <ColumnDefinition Width="83*" />
    <ColumnDefinition Width="84*" />
    <ColumnDefinition Width="84*" />
    <ColumnDefinition Width="84*" />
    <ColumnDefinition Width="84*" />
    <ColumnDefinition Width="84*" />
  </Grid.ColumnDefinitions>

  <ItemsControl ItemsSource="{Binding schedule}" 
                Name="Calender"
                VerticalAlignment="Stretch"
                Grid.ColumnSpan="7"
                Margin="0,-8,0,0">

    <ItemsControl.Template>
      <ControlTemplate TargetType="ItemsControl" >
        <Border BorderBrush="CornflowerBlue" BorderThickness="3">
          <ItemsPresenter/>
        </Border>
      </ControlTemplate>
    </ItemsControl.Template>

    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <Grid ShowGridLines="False" Name="gridCalender">
          <Grid.Background>
            <RadialGradientBrush>
              <GradientStop Color="#FFC3D6F5" Offset="0" />
              <GradientStop Color="#FFEFF5FF" Offset="1" />
            </RadialGradientBrush>
          </Grid.Background>
          <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
          </Grid.ColumnDefinitions>
        </Grid>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Border BorderThickness="0.5" BorderBrush="CornflowerBlue">
          <TextBlock OpacityMask="Black"  Name="txtBlockdays">
            <Button Content="{Binding day}"
                    Width="175"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Top"
                    VerticalContentAlignment="Top"
                    HorizontalContentAlignment="Left"
                    Name="btnCalenderDate"
                    Click="btnCalenderDate_Click"
                    Loaded="btnCalenderDate_Loaded"
                    Height="18"
                    FontSize="10"
                    FontWeight="Bold">
            </Button>
          </TextBlock>
        </Border>
      </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemContainerStyle>
      <Style >
        <Setter  Property="Grid.Column" Value="{Binding WeekDay}"  />
        <Setter Property="Grid.Row" Value="{Binding WeekNo}" />
        <Setter Property="Control.BorderBrush" Value="Black" />
      </Style>
    </ItemsControl.ItemContainerStyle>
  </ItemsControl>
</Grid>

Ответы [ 2 ]

4 голосов
/ 02 ноября 2011

Я использовал найденный код здесь , перед которым элемент управления Grid расширяется, чтобы включить свойства, которые позволяют указывать видимость, толщину и цвет GridLine

<local:GridEx ShowCustomGridLines="True" 
              GridLineBrush="#FF38B800" 
              GridLineThickness="2">
    ...
</local:GridEx>

В случае, если эта ссылка не работает, вот код:

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)));
    }
}
0 голосов
/ 02 ноября 2011

Просто добавьте дополнительные пустышки, чтобы заполнить другие ячейки ...

...