XAML binding a DynamicObject produces BindingExpression path error on Runtime


  • Hi,

    I'm trying to build a generic way to validate entered data before it gets written to the model.
    So I thought I could use DynamicObject to create a wrapper for my model classes.
    But I'm having trouble to get the XAML binding working. I always get a "BindingExpression path error". 
    The question is, can I get the binding working with a DynamicObject, or is there no way to get this working?
    If there is a way to make it work, I would appreciate if you could show me an example of how to do it.


    Wednesday, October 29, 2014 11:42 AM

All replies

  • Can you provide more context around this scenario?  Show some code so we understand exactly how you're trying to achieve this.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, October 29, 2014 1:07 PM
  • Hi,

    I created a SubClass of DynamicModel having a dictonary, I copy all the model properties into.

        class DynamicModel<T> : DynamicObject
            public DynamicModel(T model)
                _model = model;
                TypeInfo t = typeof(T).GetTypeInfo();
                foreach (PropertyInfo attribute in t.DeclaredProperties)
                    _properties.Add(attribute.Name, attribute.GetValue(model));
            private T _model;
            private Dictionary<string, object> _properties = new Dictionary<string, object>();
            public override bool TryGetMember(GetMemberBinder binder, out object result)
                return _properties.TryGetValue(binder.Name, out result);
            public override bool TrySetMember(SetMemberBinder binder, object value)
                _properties[binder.Name] = value;
                return true;
            public T Model { get { return _model; } }

    My ViewModel has a Property List that contains list of DynamicModel objects. I'm trying to get display the Name of the model object for each list element.

                            <ListView Grid.Row="1" x:Name="itemListView" ItemsSource="{Binding List}" SelectedItem="{Binding SelectedElement, Mode=TwoWay}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" >
                                    <Style TargetType="ListViewItem">
                                        <Setter Property="Margin" Value="0"></Setter>
                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                        <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                                        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                                <ColumnDefinition Width="*"/>
                                            <TextBlock Text="{Binding Name}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="10" />

    Wednesday, October 29, 2014 1:40 PM