[英]WPF Render Event Not Drawing Anything
我正在嘗試將一些WinForm代碼轉換為用於管網繪圖應用程序的WPF。 我一直以這篇繪畫應用文章為基礎:
http://www.codeproject.com/Articles/22776/WPF-DrawTools
這就是我在WinForms中所擁有的,並且由於我們需要更多可自定義的窗口,因此我試圖將其轉換。 我需要執行以下操作:
a)單擊畫布以繪制節點b)單擊並拖動上述節點c)懸停並突出顯示節點d)用鏈接連接節點
我有以下代碼在畫布上繪制矩形,但是在渲染渲染后畫布上什么也不會出現。 我相對確定它已被觸發,因為在其中放置一個消息框會導致程序崩潰。
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Colors.LimeGreen;
Pen myPen = new Pen(Brushes.Blue, 10);
Rect myRect = new Rect(50, 50, 500, 500);
drawingContext.DrawRectangle(mySolidColorBrush, myPen, myRect);
}
private void myCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Forms.MessageBox.Show("click event fired");
DrawingVisual vs = new DrawingVisual();
DrawingContext dc = vs.RenderOpen();
OnRender(dc);
}
“已觸發”消息框就位於其中,以確保觸發click事件,並且確實如此。 XML:
<TabItem Header="View Results">
<Canvas Background="WhiteSmoke" Name="myCanvas" MouseDown="myCanvas_MouseDown" >
</Canvas>
</TabItem>
是什么賦予了? 這篇文章中的人使用了用戶控件...這就是為什么我遇到問題嗎? WPF使我發瘋……我感覺好像在做完全錯誤的事情,但是關於該主題的文檔很少。
看這是我在20分鍾內完成的一個簡單示例:
XAML:
<Window x:Class="NodesEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NodesEditor"
Title="Window1" Height="800" Width="800" x:Name="view">
<Grid Margin="10">
<Grid.Resources>
<!-- This CompositeCollection basically Concatenates the Nodes and Connectors in a single one -->
<CompositeCollection x:Key="Col">
<CollectionContainer Collection="{Binding DataContext.Connectors,Source={x:Reference view}}"/>
<CollectionContainer Collection="{Binding DataContext.Nodes,Source={x:Reference view}}"/>
</CompositeCollection>
<!-- This is the DataTemplate that will be used to render the Node class -->
<DataTemplate DataType="{x:Type local:Node}">
<Thumb DragDelta="Thumb_Drag">
<Thumb.Template>
<ControlTemplate TargetType="Thumb">
<Ellipse Height="10" Width="10" Stroke="Black" StrokeThickness="1" Fill="Blue"
Margin="-5,-5,5,5" x:Name="Ellipse"/>
<ControlTemplate.Triggers>
<Trigger Property="IsDragging" Value="True">
<Setter TargetName="Ellipse" Property="Fill" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</DataTemplate>
<!-- This is the DataTemplate that will be used to render the Connector class -->
<DataTemplate DataType="{x:Type local:Connector}">
<Line Stroke="Black" StrokeThickness="1"
X1="{Binding Start.X}" Y1="{Binding Start.Y}"
X2="{Binding End.X}" Y2="{Binding End.Y}"/>
</DataTemplate>
</Grid.Resources>
<!-- This Border serves as a background and the VisualBrush used to paint its background serves as the "Snapping Grid" -->
<!-- The "Snapping" Actually occurs in the Node class (see Node.X and Node.Y properties), it has nothing to do with any UI Elements -->
<Border>
<Border.Background>
<VisualBrush TileMode="Tile"
Viewport="0,0,50,50" ViewportUnits="Absolute"
Viewbox="0,0,50,50" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Rectangle Stroke="Darkgray" StrokeThickness="1" Height="50" Width="50"
StrokeDashArray="5 3"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>
<ItemsControl>
<ItemsControl.ItemsSource>
<StaticResource ResourceKey="Col"/>
</ItemsControl.ItemsSource>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
背后的代碼:
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace NodesEditor
{
public partial class MainWindow : Window
{
public List<Node> Nodes { get; set; }
public List<Connector> Connectors { get; set; }
public MainWindow()
{
InitializeComponent();
Nodes = NodesDataSource.GetRandomNodes().ToList();
Connectors = NodesDataSource.GetRandomConnectors(Nodes).ToList();
DataContext = this;
}
private void Thumb_Drag(object sender, DragDeltaEventArgs e)
{
var thumb = sender as Thumb;
if (thumb == null)
return;
var data = thumb.DataContext as Node;
if (data == null)
return;
data.X += e.HorizontalChange;
data.Y += e.VerticalChange;
}
}
}
資料模型:
public class Node: INotifyPropertyChanged
{
private double _x;
public double X
{
get { return _x; }
set
{
//"Grid Snapping"
//this actually "rounds" the value so that it will always be a multiple of 50.
_x = (Math.Round(value / 50.0)) * 50;
OnPropertyChanged("X");
}
}
private double _y;
public double Y
{
get { return _y; }
set
{
//"Grid Snapping"
//this actually "rounds" the value so that it will always be a multiple of 50.
_y = (Math.Round(value / 50.0)) * 50;
OnPropertyChanged("Y");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Connector
{
public Node Start { get; set; }
public Node End { get; set; }
}
隨機數據源(用示例填充示例)
using System;
using System.Collections.Generic;
using System.Linq;
namespace NodesEditor
{
public static class NodesDataSource
{
public static Random random = new Random();
public static Node GetRandomNode()
{
return new Node
{
X = random.Next(0,500),
Y = random.Next(0,500)
};
}
public static IEnumerable<Node> GetRandomNodes()
{
return Enumerable.Range(5, random.Next(6, 10)).Select(x => GetRandomNode());
}
public static Connector GetRandomConnector(IEnumerable<Node> nodes)
{
return new Connector { Start = nodes.FirstOrDefault(), End = nodes.Skip(1).FirstOrDefault() };
}
public static IEnumerable<Connector> GetRandomConnectors(List<Node> nodes)
{
var result = new List<Connector>();
for (int i = 0; i < nodes.Count() - 1; i++)
{
result.Add(new Connector() {Start = nodes[i], End = nodes[i + 1]});
}
return result;
}
}
}
這是我的計算機中的樣子:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.