custom control problems

i am writing a custom control to draw a series of polylines.

i've managed to create a nice little program that does just that (and also draws the equivalent bar chart :-) ) but i am having problems converting this to a custom control.

my first problem is that i do not know how to create DP's for an unknown number of user defined variables. for example i want the user to be able to say something like:

<LineChart .........................>
<Line>
<"series of points"/>
</Line>
<Line>
<"series of points"/>
</Line>
..................
</LineChart>

(the reason for having any number of lines is that i want to be able to add and delete lines at runtime)

my initial thought was to use a class to define each line but i am having trouble coding this.

my next problem is that, if the number of lines is not fixed, how do i actually create them? for example in my generic.xaml file i cannot say
<Canvas>
<Polyline ....................../>
<Polyline ....................../>
</Canvas>
because i do not know how many polylines to create until run time.

so i thought i would create the polylines in the code and then add them to the screen.

however i cannot work out how to reference the canvas (from generic.xaml) where i want the polylines to be added.
normally, in a simple WPF, i would just reference the name and say
canvas1.add(polyline) etc
but the CS code doesnt seem to know anything about the generic.xaml file

any help is appreciate :-)

[1623 byte] By [SPWilkinson] at [2007-12-24]
# 1

I did something like this sometime back like this.

had a canvas, loop through the data and create the lines in the code.

you can use FindName to get a reference to the canvas

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
thanks for your answer

ive used:
Canvas c = (Canvas)FindName("canvas1");
c.Children.Add(p);

where p is a polyline, but i get a null reference exception thrown on c.children....

my canvas in generic.xaml is named:

<Canvas x:Name="canvas1" Width="300" Height="100" Opacity="0.5" Style="{StaticResource myCanvasStyle}">

and ive also tried Name="canvas1" instead of x:Name but to no avail

thanks,

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Try this

Canvas ChartCanvas = this.Template.FindName("canvas1", this) as Canvas;

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4
thanks for your help lee but again i get the same error!?

System.NullReferenceException occurred
Message="Object reference not set to an instance of an object."
Source="LineChartCustomControl"

stu

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

I was inheriting from ItemsControl. I did not have any problem doing that, I am using June CTP

<Style TargetType="{x:Type local:Chart}">

...

<Canvas Grid.Row="1" Name="ChartCanvas" Grid.Column="1" Background="{StaticResource GridDrawingBrushResource}" >

<Path Stroke="Black" StrokeThickness="1" Name="path1">

</Path>

</Canvas>

...

</Style>

and in codebehind

Canvas ChartCanvas = this.Template.FindName("ChartCanvas", this) as Canvas;

_chartHeight = grid1.RowDefinitions[1].ActualHeight;

_chartWidth = grid1.ColumnDefinitions[1].ActualWidth;

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6
this is my xaml code:

<Style TargetType="{x:Type local:LineChart}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LineChart}">
<Canvas Name="canvas1" Width="300" Height="100" Opacity="0.5" Style="{StaticResource myCanvasStyle}">
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

and the relevant CS code in the LineChart.cs file:

Canvas canvas1 = this.Template.FindName("canvas1", this) as Canvas;
canvas1.Children.Add(p);

and i still get the same error?!?!
i am not 100% sure i have got the june ctp (although i am 90% sure).
i will check my code on another PC that i am sure has the june ctp and then post again.

thanks again for your help! :-)

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7
In which event are you trying to get the ref. to canvas. we should do it after the template is applied
leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8
i am doing it in the constructor for the LinChart

public LineChart()
{
SetupBindings();
Polyline p = new Polyline();
p.Points.Add(new Point(0, 50));
p.Points.Add(new Point(10, 0));
p.Points.Add(new Point(100, 250));
SolidColorBrush scb = new SolidColorBrush();
Color col = new Color();
col.B = Convert.ToByte(55);
col.R = Convert.ToByte(14);
col.G = Convert.ToByte(240);
Brush b = new SolidColorBrush(col);
p.StrokeThickness = 10;

p.Stroke = b;

Canvas canvas1 = this.Template.FindName("canvas1", this) as Canvas;
canvas1.Children.Add(p);
}

is this wrong?

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 9

try to hookup to loaded event and do it there

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 10
sorry could you explain what you mean by hook up to the loaded event?

you mean when the Linechart is loaded? ie constructor is called?

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 11

public LineChart()
{
SetupBindings();
Polyline p = new Polyline();
p.Points.Add(new Point(0, 50));
p.Points.Add(new Point(10, 0));
p.Points.Add(new Point(100, 250));
SolidColorBrush scb = new SolidColorBrush();
Color col = new Color();
col.B = Convert.ToByte(55);
col.R = Convert.ToByte(14);
col.G = Convert.ToByte(240);
Brush b = new SolidColorBrush(col);
p.StrokeThickness = 10;

p.Stroke = b;
this.Loaded += new RoutedEventHandler(Chart_Loaded);

}

void Chart_Loaded(object sender, RoutedEventArgs e)
{
Canvas canvas1 = this.Template.FindName("canvas1", this) as Canvas;
canvas1.Children.Add(p);


}

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 12
thanks very much for your help lee that seems to have done the trick!!

for some reason the polyline isnt showing but ive added a textblock and that is showing so its probs something stupid ive done

thanks again, stu

SPWilkinson at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified