@@ -36,7 +36,7 @@ public class XamlEditOperations
3636 readonly XamlParserSettings _settings ;
3737
3838
39- readonly char _delimeter = Convert . ToChar ( 0x7F ) ;
39+ static readonly char _delimeter = Convert . ToChar ( 0x7F ) ;
4040
4141 /// <summary>
4242 /// Delimet character to seperate different piece of Xaml's
@@ -105,91 +105,124 @@ public void Copy(ICollection<DesignItem> designItems)
105105 }
106106
107107 /// <summary>
108- /// Paste items from clipboard into the designer .
108+ /// Paste items from clipboard into the PrimarySelection .
109109 /// </summary>
110110 public void Paste ( )
111111 {
112+ this . Paste ( _context . Services . Selection . PrimarySelection ) ;
113+ }
114+
115+ /// <summary>
116+ /// Paste items from clipboard into the container.
117+ /// </summary>
118+ public void Paste ( DesignItem container )
119+ {
120+ var parent = container ;
121+ var child = container ;
122+
112123 bool pasted = false ;
113124 string combinedXaml = Clipboard . GetText ( TextDataFormat . Xaml ) ;
114125 IEnumerable < string > xamls = combinedXaml . Split ( _delimeter ) ;
115126 xamls = xamls . Where ( xaml => xaml != "" ) ;
116127
117- DesignItem parent = _context . Services . Selection . PrimarySelection ;
118- DesignItem child = _context . Services . Selection . PrimarySelection ;
119-
120- XamlDesignItem rootItem = _context . RootItem as XamlDesignItem ;
128+
129+ XamlDesignItem rootItem = parent . Services . DesignPanel . Context . RootItem as XamlDesignItem ;
121130 var pastedItems = new Collection < DesignItem > ( ) ;
122- foreach ( var xaml in xamls ) {
131+ foreach ( var xaml in xamls )
132+ {
123133 var obj = XamlParser . ParseSnippet ( rootItem . XamlObject , xaml , _settings ) ;
124- if ( obj != null ) {
125- DesignItem item = _context . _componentService . RegisterXamlComponentRecursive ( obj ) ;
134+ if ( obj != null )
135+ {
136+ DesignItem item = ( ( XamlComponentService ) parent . Services . Component ) . RegisterXamlComponentRecursive ( obj ) ;
126137 if ( item != null )
127138 pastedItems . Add ( item ) ;
128139 }
129- }
130-
131- if ( pastedItems . Count != 0 ) {
132- var changeGroup = _context . OpenGroup ( "Paste " + pastedItems . Count + " elements" , pastedItems ) ;
133- while ( parent != null && pasted == false ) {
134- if ( parent . ContentProperty != null ) {
135- if ( parent . ContentProperty . IsCollection ) {
136- if ( CollectionSupport . CanCollectionAdd ( parent . ContentProperty . ReturnType , pastedItems . Select ( item => item . Component ) ) && parent . GetBehavior < IPlacementBehavior > ( ) != null ) {
140+ }
141+
142+ if ( pastedItems . Count != 0 )
143+ {
144+ var changeGroup = parent . Services . DesignPanel . Context . OpenGroup ( "Paste " + pastedItems . Count + " elements" , pastedItems ) ;
145+ while ( parent != null && pasted == false )
146+ {
147+ if ( parent . ContentProperty != null )
148+ {
149+ if ( parent . ContentProperty . IsCollection )
150+ {
151+ if ( CollectionSupport . CanCollectionAdd ( parent . ContentProperty . ReturnType , pastedItems . Select ( item => item . Component ) ) && parent . GetBehavior < IPlacementBehavior > ( ) != null )
152+ {
137153 AddInParent ( parent , pastedItems ) ;
138154 pasted = true ;
139155 }
140- } else if ( pastedItems . Count == 1 && parent . ContentProperty . Value == null && parent . ContentProperty . ValueOnInstance == null && parent . View is ContentControl ) {
156+ }
157+ else if ( pastedItems . Count == 1 && parent . ContentProperty . Value == null && parent . ContentProperty . ValueOnInstance == null && parent . View is ContentControl )
158+ {
141159 AddInParent ( parent , pastedItems ) ;
142160 pasted = true ;
143161 }
144- if ( ! pasted )
145- parent = parent . Parent ;
146- } else {
162+ if ( ! pasted )
163+ parent = parent . Parent ;
164+ }
165+ else
166+ {
147167 parent = parent . Parent ;
148168 }
149169 }
150170
151- while ( pasted == false ) {
152- if ( child . ContentProperty != null ) {
153- if ( child . ContentProperty . IsCollection ) {
154- foreach ( var col in child . ContentProperty . CollectionElements ) {
155- if ( col . ContentProperty != null && col . ContentProperty . IsCollection ) {
156- if ( CollectionSupport . CanCollectionAdd ( col . ContentProperty . ReturnType , pastedItems . Select ( item => item . Component ) ) ) {
171+ while ( pasted == false )
172+ {
173+ if ( child . ContentProperty != null )
174+ {
175+ if ( child . ContentProperty . IsCollection )
176+ {
177+ foreach ( var col in child . ContentProperty . CollectionElements )
178+ {
179+ if ( col . ContentProperty != null && col . ContentProperty . IsCollection )
180+ {
181+ if ( CollectionSupport . CanCollectionAdd ( col . ContentProperty . ReturnType , pastedItems . Select ( item => item . Component ) ) )
182+ {
157183 pasted = true ;
158184 }
159185 }
160186 }
161187 break ;
162- } else if ( child . ContentProperty . Value != null ) {
188+ }
189+ else if ( child . ContentProperty . Value != null )
190+ {
163191 child = child . ContentProperty . Value ;
164- } else if ( pastedItems . Count == 1 ) {
192+ }
193+ else if ( pastedItems . Count == 1 )
194+ {
165195 child . ContentProperty . SetValue ( pastedItems . First ( ) . Component ) ;
166196 pasted = true ;
167197 break ;
168- } else
198+ }
199+ else
169200 break ;
170- } else
201+ }
202+ else
171203 break ;
172204 }
173205
174- foreach ( var pastedItem in pastedItems ) {
175- _context . _componentService . RaiseComponentRegisteredAndAddedToContainer ( pastedItem ) ;
206+ foreach ( var pastedItem in pastedItems )
207+ {
208+ ( ( XamlComponentService ) parent . Services . Component ) . RaiseComponentRegisteredAndAddedToContainer ( pastedItem ) ;
176209 }
177210
178211
179212 changeGroup . Commit ( ) ;
180213 }
181- }
182-
214+ }
215+
183216 /// <summary>
184217 /// Adds Items under a parent given that the content property is collection and can add types of <paramref name="pastedItems"/>
185218 /// </summary>
186219 /// <param name="parent">The Parent element</param>
187220 /// <param name="pastedItems">The list of elements to be added</param>
188- void AddInParent ( DesignItem parent , IList < DesignItem > pastedItems )
221+ static void AddInParent ( DesignItem parent , IList < DesignItem > pastedItems )
189222 {
190223 IEnumerable < Rect > rects = pastedItems . Select ( i => new Rect ( new Point ( 0 , 0 ) , new Point ( ( double ) i . Properties [ "Width" ] . ValueOnInstance , ( double ) i . Properties [ "Height" ] . ValueOnInstance ) ) ) ;
191224 var operation = PlacementOperation . TryStartInsertNewComponents ( parent , pastedItems , rects . ToList ( ) , PlacementType . PasteItem ) ;
192- ISelectionService selection = _context . Services . Selection ;
225+ ISelectionService selection = parent . Services . DesignPanel . Context . Services . Selection ;
193226 selection . SetSelectedComponents ( pastedItems ) ;
194227 if ( operation != null )
195228 operation . Commit ( ) ;
0 commit comments