Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
My Problem is I can not get the canvas from ItemsControl. I tried to find the solution in answers of questions which asked about similar problem, but I cannot. I tried to take image of ItemsControl but it take image with scrollviewer. I tried to get canvas object with x:Name, object was returned as null. How can I reach the Canvas Object or take the image of it?

Thanks in advance.

XML
<Style x:Key="ItemsControlStyle" TargetType="{x:Type ItemsControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ItemsControl}">
                    <Border SnapsToDevicePixels="true" removed="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ScrollViewer VerticalScrollBarVisibility="Visible">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            </ItemsPresenter>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


                <ItemsControl x:Name="ic"  DataContext="{Binding Path=CanvasViewModel}" ItemsSource="{Binding Path=Items}" Width="Auto" Height="Auto" VerticalAlignment="Stretch" 
                          HorizontalAlignment="Stretch"  Style="StaticResource ItemsControlStyle}" ItemTemplateSelector="{StaticResource ItemDatatemplateSelector}" >
                    <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas IsItemsHost="True" DataContext="{Binding ElementName=ic, Path=DataContext}" Width="Auto" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Transparent" AllowDrop="True" x:Name="Canvas"/>
                    </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                        <ItemsControl.ItemContainerStyle>
                        <Style>
                            <Setter Property="Canvas.Left" Value="{Binding Path=Left}"/>
                            <Setter Property="Canvas.Top" Value="{Binding Path=Top}"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>
Posted
Comments
Kekeli 21-Jul-14 10:25am    
Do you want to generate an image from the canvas portion of the control, or the full info (canvas with items drawn upon it)? And you want to do this in code behind, not through a FlowDocumentViewer for example?
Mahmut Koyuncu 21-Jul-14 10:51am    
I want canvas image with all items, and in codebehind. Of course, according to ActualSize of canvas.

1 solution

You might consider using RenderTargetBitmap to save it to file. See How to Render Bitmap or to Print a Visual in WPF[^]

Or you could leverage some of the XamlReader options

public static UserControl RenderUserControlFromString(string rawXamlString,
     object dataContextObject = null)
{
    var document = XamlReader.Load(new XmlTextReader(new StringReader(rawXamlString))) as UserControl;
    if (dataContextObject != null)
    {
        document.DataContext = dataContextObject;
    }

    return document;
}

public static UserControl RenderUserControlFromFile(string templatePath,
        object dataContextObject = null)
{
    string rawXamlText = string.Empty;
    using (StreamReader streamReader = File.OpenText(templatePath))
    {
        rawXamlText = streamReader.ReadToEnd();
    }

    return RenderUserControlFromString(rawXamlText, dataContextObject);
}


The above code is based on the series of articles starting with http://roecode.wordpress.com/2007/12/21/using-flowdocument-xaml-to-print-xps-documents/[^]

I'm using an ItemsControl with Canvas much as you are; I load the user control into memory and plop it into a document for viewing or printing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900