Binding does not have a Clone method, whats an effective way to copy it

前端 未结 4 1201
情歌与酒
情歌与酒 2020-12-11 03:03

I wish to copy a binding, this is so i can set a different source property on it without affecting the original binding. Is this just a case of setting all of the properties

4条回答
  •  执笔经年
    2020-12-11 03:30

    I just started using this. It's not the most efficient but is fast enough for me so far. It's simple and in theory shouldn't miss any properties.

    using System.IO;
    using System.Windows.Data;
    using System.Windows.Markup;
    using System.Xml;
    
    static Binding CloneBinding(Binding binding)
    {
        var xaml = XamlWriter.Save(binding);
        var stringReader = new StringReader(xaml);
        var xmlReader = XmlReader.Create(stringReader);
        return (Binding)XamlReader.Load(xmlReader);
    }
    

    Inspired by another question's answer.

提交回复
热议问题