类似于方格纸的外观,我想在画布的背景上绘制不同颜色的直线以形成网格.以下代码适用于仅绘制红线.我还想绘制一些蓝线和灰线.这意味着我还需要两组线,到目前为止,我还无法解决绘制新的其他颜色线的问题.
<Window x:Class="GridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="1000">
<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Canvas Width="10000" Height="10000">
<Canvas.Background>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 100,100" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,100"/>
<LineGeometry StartPoint="0,0" EndPoint="100,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Red"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
</ScrollViewer>
</Window>
Run Code Online (Sandbox Code Playgroud)
重申...这段代码正在做我想要的单色.但我也想添加不同颜色的线条.
我在XMAL文件中多次出现以下XAML片段.是否可以将ComboBox和ComboBoxItem组合成单个静态或动态资源,以节省空间并简化维护问题?
<Button>
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top">
<ComboBox Width="34" FontSize="13" Margin="0" Padding="2,0,0,0">
<ComboBoxItem Content="01"></ComboBoxItem>
<ComboBoxItem Content="02"></ComboBoxItem>
<ComboBoxItem Content="03"></ComboBoxItem>
<ComboBoxItem Content="04"></ComboBoxItem>
<ComboBoxItem Content="05"></ComboBoxItem>
<ComboBoxItem Content="06"></ComboBoxItem>
<ComboBoxItem Content="08"></ComboBoxItem>
....... ALL THE WAY TO 100 Items......
<ComboBoxItem Content="100"></ComboBoxItem>
</ComboBox>
</Canvas>
</Button>
Run Code Online (Sandbox Code Playgroud) 我在画布上有一堆线条.我想迭代线条并将它们的笔触颜色变为黑色.
foreach循环中的代码行将无法编译.
foreach (FrameworkElement Framework_Element in My_Canvas.Children)
{
//the following line of code won't compile.
(Line)Framework_Element.Stroke = new SolidColorBrush(Colors.Black);
}
Run Code Online (Sandbox Code Playgroud)