1 wpf
我的第一个WPF应用程序,但它不起作用.请帮忙!
xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="308" Width="527">
<Grid Name="canvas">
<Canvas></Canvas>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Line line = new Line();
line.X1 = 0;
line.Y1 = 100;
line.X2 = 0;
line.Y2 = 100;
line.Stroke = Brushes.Red;
line.StrokeThickness = 1; // Note1
canvas.Children.Insert(0, line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到的是你的第一个X,Y坐标和第二个是相同的.因此,绘制的线是在同一点上.
line.X1 = 0;
line.Y1 = 100;
line.X2 = 0;
line.Y2 = 100;
// Change too this and that will will draw straight over 100 pixels.
line.X1 = 0;
line.Y1 = 100;
line.X2 = 100;
line.Y2 = 100;
Run Code Online (Sandbox Code Playgroud)
您的X1/Y1值与X2/Y2值相同.如果改变line.X2 = 0; to line.X2 = 50;,你会看到你的线.
如果您的线路不是动态的,通常最好直接在XAML中执行大多数可视化工作:
<Grid Name="canvas">
<Line X1="0" Y1="100" X2="50" Y2="100" StrokeThickness="1" Stroke="Red" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,安迪