首页 / C# / c# – WPF渲染事件没有绘制任何东西
c# – WPF渲染事件没有绘制任何东西
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF渲染事件没有绘制任何东西,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6330字,纯文字阅读大概需要10分钟。
内容图文
![c# – WPF渲染事件没有绘制任何东西](/upload/InfoBanner/zyjiaocheng/816/20533ef94c9c40129eff9efb0c047f63.jpg)
我正在尝试将一些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);
}
“触发”消息框就在那里,以确保点击事件触发,并且确实如此.
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;
}
}
}
这就是我的电脑中的样子:
内容总结
以上是互联网集市为您收集整理的c# – WPF渲染事件没有绘制任何东西全部内容,希望文章能够帮你解决c# – WPF渲染事件没有绘制任何东西所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。