WPF ItemsControl图像转换问题MVVM [英] WPF ItemsControl Image Convertion Problem MVVM

查看:66
本文介绍了WPF ItemsControl图像转换问题MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的问题是我无法从ItemsControl获取画布。我试图找到解决类似问题的问题的答案,但我不能。我试图拍摄ItemsControl的图像,但它使用scrollviewer拍摄图像。我试图用x:Name获取canvas对象,对象返回null。如何到达Canvas对象或拍摄它的图像?



提前致谢。



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.

<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>

推荐答案

您可以考虑使用RenderTargetBitmap将其保存到文件。请参阅如何渲染位图或打印WPF中的视觉 [ ^ ]



或者你可以利用一些XamlReader选项



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);
}





以上代码基于以 http://roecode.wordpress.com/2007/12/21/using-flowdocument -xaml-to-print-xps-documents / [ ^ ]



我正在使用带有Canvas的ItemsControl;我将用户控件加载到内存中并将其放入文档中以供查看或打印。



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.


这篇关于WPF ItemsControl图像转换问题MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆