有一个简单/内置方式来获得一个XAML元素的精确复制(克隆)? [英] Is there an easy/built-in way to get an exact copy (clone) of a XAML element?

查看:203
本文介绍了有一个简单/内置方式来获得一个XAML元素的精确复制(克隆)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要XAML领域的打印等有使这个按钮处理程序:

 私人无效Button_Click_Print(对象发件人,RoutedEventArgs E)
{
Customer.PrintReport(PrintableArea);
}

和在PrintReport我收拾FrameworkElement的到其他要素,以打印在一个稍微的不同的办法比它在屏幕上,像这样的:

 公共无效PrintReport(FrameworkElement的FWE)
{
StackPanel的SP =新的StackPanel();
sp.Children.Add(FWE);
TextBlock的TB =新的TextBlock();
tb.Text =你好;
sp.Children.Add(TB);

PrintDialog类对话框=新PrintDialog类();
如果(dialog.ShowDialog()==真)
{
dialog.PrintVisu​​al(SP,打印作业);
}
}



但上面给了我下面的错误




指定的元素已经是另一个元素的
逻辑子。
断开它第一次。




有没有一种简单的方法来克隆FrameworkElement的,这样我可以操控副本,打印出来,然后忘掉它,留在XAML原始元素在屏幕上显示完整



这样的事情我会想象?

  FrameworkElement的fwe2 = FrameworkElement.Clone(FWE); //伪代码


解决方案

我也有类似的问题我目前的项目,并与此代码解决了这个问题。

 公共静态类ExtensionMethods 
{
公共静态Ť XamlClone< T>(这件T原件)
,其中T:类
{
如果(原== NULL)
返回NULL;

对象克隆;
使用(VAR流=新的MemoryStream())
{
XamlWriter.Save(原件,流);
stream.Seek(0,SeekOrigin.Begin);
克隆= XamlReader.Load(流);
}

如果(克隆是T)
回报率(T)的克隆;
,否则
返回NULL;
}
}

这方式,它只是出现在所有对象的方法在WPF项目中,你不需要给任何参数的方法,并返回相同的类作为原始的对象。


I need to make areas of XAML printable and so have make this button handler:

private void Button_Click_Print(object sender, RoutedEventArgs e)
{
    Customer.PrintReport(PrintableArea);
}

And in PrintReport I pack the frameworkelement into other elements in order to print it in a slightly different way than it is on the screen, like this:

public void PrintReport(FrameworkElement fwe)
{
    StackPanel sp = new StackPanel();
    sp.Children.Add(fwe);
    TextBlock tb = new TextBlock();
    tb.Text = "hello";
    sp.Children.Add(tb);

    PrintDialog dialog = new PrintDialog();
    if (dialog.ShowDialog() == true)
    { 
        dialog.PrintVisual(sp, "Print job"); 
    }
}

But the above gives me the following error:

Specified element is already the logical child of another element. Disconnect it first.

Is there an easy way to clone the FrameworkElement so that I can manipulate the copy, print it, and then forget about it, leaving the original element in the XAML being displayed on the screen intact?

Something like this I would imagine:

FrameworkElement fwe2 = FrameworkElement.Clone(fwe); //pseudo-code

解决方案

I had a similar problem in my current project and solved it with this code.

public static class ExtensionMethods
{
    public static T XamlClone<T>(this T original)
        where T : class
    {
        if (original == null)
            return null;

        object clone;
        using (var stream = new MemoryStream())
        {
            XamlWriter.Save(original, stream);
            stream.Seek(0, SeekOrigin.Begin);
            clone = XamlReader.Load(stream);
        }

        if (clone is T)
            return (T)clone;
        else
            return null;
    }
}

This way it simply appears as a method on all objects in your WPF project, you do not need to give any parameters to the method, and it returns an object of the same class as the original.

这篇关于有一个简单/内置方式来获得一个XAML元素的精确复制(克隆)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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