我花的最後一小時找到一個可靠的解決方案,但有沒有一個完美的。我停在這一點上搜索,但想告訴你,我現在的嘗試:
GridControl.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApplication1.CustomControl
{
public class GridControl : Grid
{
static GridControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GridControl),
new FrameworkPropertyMetadata(typeof(GridControl)));
}
private Pen _linesPen;
#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)
{
if (_linesPen == null)
{
_linesPen = new Pen(GridLineBrush, GridLineThickness);
}
foreach (var rowDefinition in RowDefinitions)
{
dc.DrawLine(_linesPen, new Point(0, rowDefinition.Offset),
new Point(ActualWidth, rowDefinition.Offset));
}
foreach (var columnDefinition in ColumnDefinitions)
{
dc.DrawLine(_linesPen, new Point(columnDefinition.Offset, 0),
new Point(columnDefinition.Offset, ActualHeight));
}
dc.DrawRectangle(Brushes.Transparent, _linesPen,
new Rect(0, 0, ActualWidth, ActualHeight));
}
base.OnRender(dc);
}
}
}
使用的觀點:儘管所有你想實現併網發電/表結構背後的複雜性
<Window x:Class="WpfApplication1.table"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomControl="clr-namespace:WpfApplication1.CustomControl" Title="table" Height="500" Width="600">
<Grid>
<TextBlock Text="Headline" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" />
<Slider x:Name="slider" Grid.Row="1" Minimum="1" Margin="20,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" />
<TextBlock Text="Zoom" Margin="250,33,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<ScrollViewer x:Name="ScrollViewer" Margin="20,70,20,10" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<CustomControl:GridControl ShowCustomGridLines="True" Width="{Binding ElementName=ScrollViewer, Path=ActualWidth}" Height="{Binding ElementName=ScrollViewer, Path=ActualHeight}">
<Grid.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleY="{Binding Path=Value, ElementName=slider}" ScaleX="{Binding Path=Value, ElementName=slider}" />
</Grid.LayoutTransform>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Header 1" VerticalAlignment="Center" HorizontalAlignment="Center" />
<TextBlock Grid.Column="1" Grid.Row="0" Text="Header 2" VerticalAlignment="Center" HorizontalAlignment="Center" />
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- first row -->
<TextBlock Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Header 3 (over 3 columns)" />
<!-- first column in second row -->
<TextBlock Grid.Column="0" Grid.Row="1" Text="Cell 1" HorizontalAlignment="Center" VerticalAlignment="Center" />
<!-- second column in second row -->
<TextBlock Grid.Column="1" Grid.Row="1" Text="Cell 2" HorizontalAlignment="Center" VerticalAlignment="Center" />
<!-- third column in second row -->
<TextBlock Grid.Column="2" Grid.Row="1" Text="Cell 3" HorizontalAlignment="Center" VerticalAlignment="Center" />
</CustomControl:GridControl>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Cell 1 (big)" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CustomControl:GridControl Grid.Column="1" Grid.Row="1" ShowCustomGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
</CustomControl:GridControl>
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
</CustomControl:GridControl>
</CustomControl:GridControl>
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
</CustomControl:GridControl>
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
</CustomControl:GridControl>
</CustomControl:GridControl>
</CustomControl:GridControl>
</ScrollViewer>
</Grid>
</Window>
此處的另一個任務是通過重新設置尺寸來使字體變大 – Cynede 2012-04-07 08:08:37