Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 14dd5a9

Browse files
author
gumme
committed
Added xaml parser support for collections in attached properties.
1 parent b401bc5 commit 14dd5a9

4 files changed

Lines changed: 57 additions & 11 deletions

File tree

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/ExampleService.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ public static void SetExample(DependencyObject element, string value)
2626
TestHelperLog.Log("ExampleService.SetExample");
2727
element.SetValue(ExampleProperty, value);
2828
}
29+
30+
public static readonly DependencyProperty ExampleCollectionProperty = DependencyProperty.RegisterAttached(
31+
"ExampleCollection", typeof(ExampleClassList), typeof(ExampleService)
32+
);
33+
34+
public static ExampleClassList GetExampleCollection(DependencyObject element)
35+
{
36+
TestHelperLog.Log("ExampleService.GetExampleCollection");
37+
return (ExampleClassList)element.GetValue(ExampleCollectionProperty);
38+
}
39+
40+
public static void SetExampleCollection(DependencyObject element, ExampleClassList value)
41+
{
42+
TestHelperLog.Log("ExampleService.SetExampleCollection");
43+
element.SetValue(ExampleCollectionProperty, value);
44+
}
2945
}
3046

3147
public class ExampleDependencyObject : DependencyObject

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ public void ExampleServiceTest()
221221
");
222222
}
223223

224+
[Test]
225+
public void ExampleServiceCollectionTest()
226+
{
227+
TestLoading(@"
228+
<t:ExampleDependencyObject
229+
xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation""
230+
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
231+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
232+
<t:ExampleService.ExampleCollection>
233+
<t:ExampleClassList>
234+
<t:ExampleClass OtherProp=""a""> </t:ExampleClass>
235+
<t:ExampleClass OtherProp=""b"" />
236+
<t:ExampleClass OtherProp=""c"" />
237+
</t:ExampleClassList>
238+
</t:ExampleService.ExampleCollection>
239+
</t:ExampleDependencyObject>
240+
");
241+
}
242+
224243
[Test]
225244
public void ExampleClassObjectPropWithStringValue()
226245
{

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,21 @@ bool IsFirstChildResources(XamlObject obj)
276276
obj.Properties.Where((prop) => prop.IsResources).FirstOrDefault() != null;
277277
}
278278

279-
XmlElement CreatePropertyElement()
280-
{
281-
string ns = parentObject.OwnerDocument.GetNamespaceFor(parentObject.ElementType);
282-
return parentObject.OwnerDocument.XmlDocument.CreateElement(
283-
parentObject.OwnerDocument.GetPrefixForNamespace(ns),
284-
parentObject.ElementType.Name + "." + this.PropertyName,
285-
ns
286-
);
287-
}
279+
XmlElement CreatePropertyElement()
280+
{
281+
Type propertyElementType = GetPropertyElementType();
282+
string ns = parentObject.OwnerDocument.GetNamespaceFor(propertyElementType);
283+
return parentObject.OwnerDocument.XmlDocument.CreateElement(
284+
parentObject.OwnerDocument.GetPrefixForNamespace(ns),
285+
propertyElementType.Name + "." + this.PropertyName,
286+
ns
287+
);
288+
}
289+
290+
Type GetPropertyElementType()
291+
{
292+
return this.IsAttached ? this.PropertyTargetType : parentObject.ElementType;
293+
}
288294

289295
static XmlNode FindChildNode(XmlNode node, string localName, string namespaceURI)
290296
{
@@ -305,7 +311,8 @@ internal void AddChildNodeToProperty(XmlNode newChildNode)
305311
{
306312
if (this.IsCollection) {
307313
if (IsNodeCollectionForThisProperty(newChildNode)) {
308-
XmlNode parentNode = FindChildNode(parentObject.XmlElement, parentObject.ElementType.Name + "." + this.PropertyName, parentObject.OwnerDocument.GetNamespaceFor(parentObject.ElementType));
314+
Type propertyElementType = GetPropertyElementType();
315+
XmlNode parentNode = FindChildNode(parentObject.XmlElement, propertyElementType.Name + "." + this.PropertyName, parentObject.OwnerDocument.GetNamespaceFor(propertyElementType));
309316

310317
if (parentNode == null) {
311318
parentNode = CreatePropertyElement();

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlPropertyInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal class XamlDependencyPropertyInfo : XamlPropertyInfo
3939
{
4040
readonly DependencyProperty property;
4141
readonly bool isAttached;
42+
readonly bool isCollection;
4243

4344
public override DependencyProperty DependencyProperty {
4445
get { return property; }
@@ -49,6 +50,7 @@ public XamlDependencyPropertyInfo(DependencyProperty property, bool isAttached)
4950
Debug.Assert(property != null);
5051
this.property = property;
5152
this.isAttached = isAttached;
53+
this.isCollection = CollectionSupport.IsCollectionType(property.PropertyType);
5254
}
5355

5456
public override TypeConverter TypeConverter {
@@ -84,7 +86,9 @@ public override bool IsAttached {
8486
}
8587

8688
public override bool IsCollection {
87-
get { return false; }
89+
get {
90+
return isCollection;
91+
}
8892
}
8993

9094
public override object GetValue(object instance)

0 commit comments

Comments
 (0)