我正在尝试将调度控件设置为与 Outlook 中的调度。
我知道devexpress在他的图书馆中有控制权,但我不愿意为这种控制支付那么多(每年)。
现在我需要做的下一件事实际上是让网格单元格可点击。有没有人有这样做的想法?
这是我现在的代码(请记住,我稍后会将其转换为动态响应的自定义控件)。
public class BorderGrid : Grid
{
protected override void OnRender(DrawingContext dc)
{
double leftOffset = 0;
double topOffset = 0;
Pen pen = new Pen(Brushes.Gray, 0.5);
pen.Freeze();
foreach (RowDefinition row in RowDefinitions)
{
dc.DrawLine(pen, new Point(0, topOffset), new Point(this.ActualWidth, topOffset));
topOffset += row.ActualHeight;
}
foreach (ColumnDefinition column in ColumnDefinitions)
{
dc.DrawLine(pen, new Point(leftOffset, 0), new Point(leftOffset, ActualHeight));
leftOffset += column.ActualWidth;
}
base.OnRender(dc);
}
}
bordergrid文件:
{{1}}
我想从哪里开始:
答案 0 :(得分:1)
现在我需要做的下一件事实际上是让网格单元可以点击。有没有人知道这样做?
您可以使用此处的解决方案覆盖OnMouseDown
方法获取鼠标指针相对于Grid
的位置:
然后,您可以在相应的单元格中插入元素。请参阅以下示例代码。
public class BorderGrid : Grid
{
public BorderGrid()
{
Background = Brushes.Transparent;
}
protected override void OnRender(DrawingContext dc)
{
double leftOffset = 0;
double topOffset = 0;
Pen pen = new Pen(Brushes.Gray, 0.5);
pen.Freeze();
foreach (RowDefinition row in RowDefinitions)
{
dc.DrawLine(pen, new Point(0, topOffset), new Point(this.ActualWidth, topOffset));
topOffset += row.ActualHeight;
}
foreach (ColumnDefinition column in ColumnDefinitions)
{
dc.DrawLine(pen, new Point(leftOffset, 0), new Point(leftOffset, ActualHeight));
leftOffset += column.ActualWidth;
}
base.OnRender(dc);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Point point = Mouse.GetPosition(this);
int row = 0;
int col = 0;
double accumulatedHeight = 0.0;
double accumulatedWidth = 0.0;
foreach (var rowDefinition in RowDefinitions)
{
accumulatedHeight += rowDefinition.ActualHeight;
if (accumulatedHeight >= point.Y)
break;
row++;
}
foreach (var columnDefinition in ColumnDefinitions)
{
accumulatedWidth += columnDefinition.ActualWidth;
if (accumulatedWidth >= point.X)
break;
col++;
}
//color cell Red:
Grid childGrid = new Grid();
childGrid.Background = Brushes.Red;
Grid.SetColumn(childGrid, col);
Grid.SetRow(childGrid, row);
Children.Add(childGrid);
}
请注意,您必须将Grid的Background属性设置为Brush才能触发鼠标事件。