Handling WPF Canvas Resize Correctly/Efficiently
I am attaching working code. My Question: am I handling resize correctly/efficiently? I Draw a scene on my WPF Canvas Circle at Center of Canvas and a Vertical and Horizontal line in the center of the Canvas. When I resize the canvas I Clear all the Children and Redraw the entire scene based on the new Canvas Size. Is there a better way of doing this with say RenderTransform etc? FYI My real App is much more complex than this simple example I just wanted to post this code because it demonstrates the shortest code to demonstrate the real optimization I am attempting.
XAML:
<Window x:Class="Sample2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Sample2"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400" SizeChanged="Window_SizeChanged">
<Grid>
<Canvas x:Name="MainCanvas">
</Canvas>
</Grid>
</Window>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Sample2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DrawScene()
{
MainCanvas.Children.Clear();
double ctrX = MainCanvas.ActualWidth / 2;
double ctrY = MainCanvas.ActualHeight / 2;
Ellipse elps = new Ellipse();
elps.Width = elps.Height = 100;
elps.Stroke = Brushes.Red;
MainCanvas.Children.Add(elps);
Canvas.SetLeft(elps, ctrX-elps.Width/2);
Canvas.SetTop(elps, ctrY-elps.Height/2);
Line lnVertical = new Line();
lnVertical.Stroke = Brushes.Blue;
lnVertical.X1 = lnVertical.X2 = ctrX;
lnVertical.Y1 = 0;
lnVertical.Y2 = MainCanvas.ActualHeight;
MainCanvas.Children.Add(lnVertical);
Line lnHorizontal = new Line();
lnHorizontal.Stroke = Brushes.Red;
lnHorizontal.X1 = 0;
lnHorizontal.X2 = MainCanvas.ActualWidth;
lnHorizontal.Y1 = lnHorizontal.Y2 = ctrY;
MainCanvas.Children.Add(lnHorizontal);
}
//private void Window_Loaded(object sender, RoutedEventArgs e)
//{
//}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
DrawScene();
}
}
}
My Actual App is data Driven and looks like this. The only real Data Driven Elements are (Color/Type) and Depth in the Well Pipe: The Circle positions are calcd in VM for position so as to avoid overlap
I found the ultimate answer to this question through talking to a friend. clemens was nearby and probably created a very efficient answer to the question if I were able to use a single stroke color. The resize event is handled in my application no with a simple Invalidate for each child framework elelent.
SizeChanged += (s, e) =>
{
WellCanvas.SetCoordinateSystem(wm.OverallStartDepth,
wm.OverallEndDepth,
wm.OverallEndDepth,
wm.OverallStartDepth);
**// This seemed to solve the problem**
foreach (FrameworkElement elem in WellCanvas.Children)
elem.InvalidateVisual();
};
链接地址: http://www.djcxy.com/p/53312.html
上一篇: WPF居中文本拉伸标签固定宽度