WPF渲染事件不领取任何 [英] WPF Render Event Not Drawing Anything

查看:176
本文介绍了WPF渲染事件不领取任何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些WinForm的code到WPF的管网图纸的应用程序。我一直立足而过这种涂料的应用文章:

I'm trying to convert some WinForm code to WPF for a pipe network drawing app. I've been basing it off this paint app article:

HTTP://www.$c$cproject.com/Articles/ 22776 / WPF-DrawTools

这是我在WinForms和我想是因为我们需要更多的自定义窗口,将其转换过来。我需要做到以下几点:

This is what I had in WinForms and I'm trying to convert it over because we need more customizable windows. I need to do the following:

a)单击画布绘制节点 b)点击并拖动上述节点 C)将鼠标悬停在和突出节点 D)连接与连接节点

a) Click the canvas to draw nodes b) Click and drag aforementioned nodes c) Hover over and highlight nodes d) connect nodes with links

我有以下的code画在画布上的一个矩形,但什么都不会出现在画布上绘制时触发。我比较肯定它解雇了,因为把一个消息框,它会导致程序崩溃。

I have the following code to draw a rectangle on a canvas, but nothing will appear on the canvas when render is fired. I'm relatively sure it's fired because putting a message box in it causes the program to crash.

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:

The "fired" message box is just in there to make sure the click event fires, and it does. XML:

<TabItem Header="View Results">
            <Canvas Background="WhiteSmoke" Name="myCanvas" MouseDown="myCanvas_MouseDown" >                    
            </Canvas>
</TabItem>

怎么办?在文章的人使用用户的控制......这是为什么我有问题? WPF驱使我坚果...我觉得如果我做一些完全错误的,但很少有文件,我可以找到关于这个问题的。

What gives? The guy in the article uses a user control... is that why I'm having problems? WPF drives me nuts... I feel as if I am doing something completely wrong but there is very little documentation that I can find on the subject.

推荐答案

你看,这是一个简单的例子,我在20分钟内制作:

Look this is a simple example I made in 20 minutes:

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>

code背后:

Code Behind:

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; }
}

随机数据源(要填的东西的例子)

Random Data Source (To fill the example with something)

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;
        }
    }
}

这是什么样子在我的电脑:

This is what it looks like in my Computer:

这篇关于WPF渲染事件不领取任何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆