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 :-)
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,
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! :-)
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?
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);
}