0

can i create a view at design time, using dynamic objects? (with visual studio 2010)

for example, or something, is that possible?

         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:dyn="clr-namespace:System.Dynamic;assembly=System.Core"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         mc:Ignorable="d" 

and

<d:DesignProperties.DataContext>
    <dyn:ExpandoObject>
        <dyn:ExpandoObject.Name>MyName</dyn:ExpandoObject.Name>
    </dyn:ExpandoObject>
</d:DesignProperties.DataContext>

The above example does not work.

Sometimes I have class complicated to manage. And I can not use them at design time, the idea of using a dynamic type would be not to change the bindings (properties that I will use) and get a vision.

I do not know if I was clear, but if you have something that can help me would be great.

H.B.
  • 166,899
  • 29
  • 327
  • 400
J. Lennon
  • 3,311
  • 4
  • 33
  • 64

1 Answers1

1

That should not work in any case, be it design or run-time.

You can use ExpandoObjects, but only via dictionary syntax:

<dyn:ExpandoObject>
    <sys:String x:Key="Name">MyName</sys:String>
</dyn:ExpandoObject>

Though i do not know if that junk of a GUI designer is able to handle that, nor do i know if this works with compiled XAML.

Edit: As i thought you cannot compile this because some component fails:

Error 1 The Key attribute can only be used on a tag contained in a Dictionary (such as a ResourceDictionary).

Well, it is a bloody dictionary alright...


You can use a markup extension though, you can get the DictionaryFactoryExtension from this answer and modify it like this:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var expando = (IDictionary<string,object>)new ExpandoObject();
    foreach (DictionaryEntry kvp in Dictionary)
        expando[(string)kvp.Key] = kvp.Value;

    return expando;
}

//The designer uses this for whatever reason...
public object this[object key]
{
    get { return this.Dictionary[key]; }
    set { this.Dictionary[key] = value; }
}

Then you can use that just like an ExpandoObject:

<!-- Of course you can also hard-code the key-type in the class,
     it should be a string in all cases when using an ExpandoObject anyway -->
<local:DictionaryFactory KeyType="sys:String" ValueType="sys:Object">
    <sys:String x:Key="Name">MyName</sys:String>
</local:DictionaryFactory>

The designer should be able to handle this.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400