Reputation: 127
Okay so the only way to draw in wpf I know of is by adding your shape after setting it up to grid, panel etc as a child. I've tried adding them to my grid after making them public in another class but only ended up making an infinite loop. So my question is how do I draw from another class on grid in wpf efficiently.
Upvotes: 0
Views: 616
Reputation: 91
You have to think of this differently. WPF is built to work best with the MVVM style of programming. The View which is where your grid is located in XAML code needs to bind to properties on the ViewModel which contains the business logic. This will allow you to update the properties from another class which will then update the View which is bound to that property.
Is a good article on starting out: http://www.codeproject.com/Articles/819294/WPF-MVVM-step-by-step-Basics-to-Advance-Level
If your getting into WPF I also recommend buying the book: Windows Presentation 4.5 Cookbook by Pavel Yosifovich
Your grid should draw based on changing bound properties in the ViewModel. For example, you could have a line shape move around the screen based on what its bound to. Your MainWindow would look like this:
<Window x:Class="Grid_Drawing.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:Grid_Drawing"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Canvas Height="300" Width="300">
<Line X1="{Binding StartX1}"
X2="{Binding EndX2}"
Y1="{Binding StartY1}"
Y2="{Binding EndY2}"
Stroke="Red"/>
</Canvas>
</Grid>
</Window>
Then you would have the MainWindow's DataContext set (usually from App.Xaml.cs) to a ViewModel Class, called something like DrawingViewModel which inherits from INotifyPropertyChanged (the most important interface in WPF). Your class would look like:
public class DrawingViewModel : INotifyPropertyChanged
{
private double _startX1;
private double _endX2;
private double _startY1;
private double _endY2;
// properties
public double StartX1
{
get { return _startX1; }
set
{
if (_startX1 != value)
{
_startX1 = value;
OnPropertyChanged("StartX1");
}
}
}
public double EndX2
{
get { return _endX2; }
set
{
if (_endX2 != value)
{
_endX2 = value;
OnPropertyChanged("EndX2");
}
}
}
public double StartY1
{
get { return _startY1; }
set
{
if (_startY1 != value)
{
_startY1 = value;
OnPropertyChanged("StartY1");
}
}
}
public double EndY2
{
get { return _endY2; }
set
{
if (_endY2 != value)
{
_endY2 = value;
OnPropertyChanged("EndY2");
}
}
}
// end properties
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then you can modify the Start or End coordinates of the line from this class or another class if you pass the DrawingViewModel as a reference. I hope this helps.
Upvotes: 0
Reputation: 67382
So my question is how do I draw from another class on grid in wpf efficiently.
You don't. You expose methods to pass data to the class that encapsulates the grid, and the class will perform validation, and if proper it will display the data as it sees fit. This way you achieve separation of the model from the controller.
Okay so the only way to draw in wpf I know of is by adding your shape after setting it up to grid, panel etc as a child
WPF is perfectly capable of displaying writeable bitmaps, as well as GDI+ bitmaps. In fact this would be way preferable over adding multiple shape objects for your GPU to process and render over and over again.
Upvotes: 1