views:

345

answers:

2

I currently have a canvas which contains a bunch of sqaures as its children. These sqaures sit on different lines. I would like to draw a background for the canvas which draws lines (like a notepad would have feint blue lines on the paper) I would like to draw this dynamically by binding it to a collection of "lines" So if there are 2 lines in the collection, 2 lines will be drawn on the background of the canvas. I was looking into using DrawingBrush, but i am not sure if this is the correct way forward

<DrawingBrush>
  <DrawingBrush.Drawing>
    <Line Name=Line1/>
    <Line Name=Line2/>
  </DrawingBrush.Drawing>
</DrawingBrush>

(BTW The above code does not work, it is just to explain the conecpt)

+1  A: 

Try this approach. Use a new class for your canvas:

internal class SpecialCanvas : Canvas
{
    ...

    ObservableCollection<Line> Lines {get; set;}

    DrawingVisual backgroundVisual = new DrawingVisual;

    public SpecialCanvas()
    {
        this.Background = new VisualBrush(backgroundVisual);   
    }

    private void OnLinesChanged(...)
    {
       using (DrawingContext dc = this.backgroundVisual.RenderOpen())
       {
           // Draw your lines to dc here.
       }
    }

}

SMART_n
This is a simmilar solution to what i was looking for, but can i use the DrawingVisual as a Background? I Dont want the line to be a child element of the Canvas(THats because i am handling drag events on the canvas and i do not want the lines to be dragged).
Eli Perpinyal
Yes you can. Just use VisualBrush as background.
SMART_n
There is no Draw() method in the DrawingVisual class?
Eli Perpinyal
Also i cannot use a DrawingVisual as a background directly i get the following error:Cannot convert the value in attribute 'Background' to object of type 'System.Windows.Media.Brush'. 'System.Windows.Media.DrawingVisual' is not a valid value for property 'Background'Is it possible to use the DrawingVisual as a background?
Eli Perpinyal
Really, there is no Draw() method in DrawingVisual, it was my mistake. Sorry. You need to draw to your visual when your lines collection changed.
SMART_n
You cannot set a DrawingVisual for background, you need to create a VisualBrush with your visual.
SMART_n
A: 

There are a lot of ways you could possibly do what you want to do. For a simple XAML only solution, you could just use an itemscontrol.

<Window.Resources>
    <x:Array x:Key="Lines" Type="{x:Type Line}"> 
        <Line X1="0" X2="400" Y1="25" Y2="25" Stroke="Black" />
        <Line X1="0" X2="400" Y1="25" Y2="25" Stroke="Black" />
    </x:Array>

</Window.Resources>
<Canvas>
    <ItemsControl ItemsSource="{StaticResource Lines}" />

    <Rectangle Height="20" Width="20" Canvas.Left="20" Canvas.Top="5" Stroke="Blue" Fill="Blue" />
    <Rectangle Height="20" Width="20" Canvas.Left="120" Canvas.Top="5" Stroke="Blue" Fill="Blue" />
    <Rectangle Height="20" Width="20" Canvas.Left="20" Canvas.Top="30" Stroke="Blue" Fill="Blue" />
    <Rectangle Height="20" Width="20" Canvas.Left="120" Canvas.Top="30" Stroke="Blue" Fill="Blue" />
</Canvas>
mdm20