arbonix-allison
Topic Author
Posts: 1
Joined: 10 Apr 2024, 19:47

Bindings to dynamic properties on a DynamicObject failing in Noesis, but not Windows WPF

25 Sep 2024, 22:37

I am creating Binding objects in C#, like so:
            TextBlock textBlock = new TextBlock();
            textBlock.DataContext = dataContext;
            Binding binding = new Binding("ButtonPressCount")
            {
                StringFormat = "Button press count, read directly off parent object (no proxy): {0}"
            };
            textBlock.SetBinding(TextBlock.TextProperty, binding);
Now, if `dataContext` is a plain object with a property on it called `ButtonPressCount`, we're all good! But, if it's a DynamicObject that resolves `ButtonPressCount` via `TryGetMember`, it breaks and says that `ButtonPressCount` doesn't exist.

It works fine in Blend.

Thanks in advance!
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: Bindings to dynamic properties on a DynamicObject failing in Noesis, but not Windows WPF

02 Oct 2024, 10:58

Hello,

We haven't ever tried using DynamicObjects in Noesis. But in our current implementation, when a C# object is passed to our library we generate a Type representation in the native side by using C# reflection (Type.GetProperties). So only the properties that are defined at that point would be available in bindings.

For this DynamicObject to work I guess we will need some sort of find property fallback, that can check if a new property was added after native Type creation. We need to think about this, could you please report it in our bugtracker?

An alternative solution is to use an indexer (like a Dictionary) with string keys (which will be the property names):
public class MyDynamicObject
{
    private Dictionary<string, object> _values = new();

    public object this[string s]
    {
        get { return _values.TryGetValue(s, out val) ? val : null; }
        set { _values[s] = value; }
    }
}
<TextBlock Text="{Binding SomeDynamicObject[ButtonPressCount]}"/>
Could that work for you?

Who is online

Users browsing this forum: Bing [Bot] and 4 guests