在 ItemsControl WPF 上设置网格线样式

发布于 2024-12-13 10:43:12 字数 3556 浏览 0 评论 0原文

截至目前,在我的日历上,仅在该月所需的文本块(日期)周围放置边框。我有一个 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>

As of now on my calender, a border is onlyplaced around the textblocks(dates) that are needed for that month. I have a grid that has 7 columns and 6 rows so thats 42 cells. A month only has max 31days(cells) so I have a lot of empty cells that dont have a border around it. How can I change this so that all 42 cells has a border around it making it look how a calender should look. Thanks in advance. :)

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-12-20 10:43:12

我使用了此处 之前扩展了 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)));
    }
}

I've used the code found here before which extends the Grid control to include properties that allow you to specify GridLine visibility, thickness, and color

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

In case that link goes dead, here's the code:

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)));
    }
}
北座城市 2024-12-20 10:43:12

只需添加额外的虚拟项目来填充其他单元格......

Just add extra dummy items to fill the other cells...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文