Wpf: Binding to ExpandoObject Properties throws Binding Exception

Created on 16 May 2019  路  3Comments  路  Source: dotnet/wpf

  • .NET Core Version: 3.0.100-preview5-011568, Commit: b487ff10aa
    OS Name: Windows
    OS Version: 10.0.15063
    OS Platform: Windows
    RID: win10-x64
    Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview5-011568\

Host (useful for support):
Version: 3.0.0-preview5-27626-15
Commit: 61f30f5a23

  • Windows version: Windows 10 Version 1703 (Build: 15063.1746)
  • Visual Studio version: 16.1.0 Preview 3
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No, Binding works as expected

    Problem description:
    Binding an ExpandoObject in WPF Control (ex. Textbox.Text) throws BindingExpression Error in .NET Core WPF, but Binding works in .NET Framework (4.8 and 4.7.2)

    Actual behavior:
    Throwing BindingExpression Error
    System.Windows.Data Error: 40
    BindingExpression path error: 'Test' property not found on 'object' ''ExpandoObject' (HashCode=58204539)'. BindingExpression:Path=TestProperty.Test; DataItem='MainViewModel' (HashCode=16745860); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

    Expected behavior:
    Binding Property as in .NET Framework

    Minimal repro:

using System.ComponentModel;
using System.Dynamic;
using System.Runtime.CompilerServices;

namespace WpfApp1.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            TestProperty = new ExpandoObject();
            TestProperty.Test = "abc";
            TestProperty.ABC = "xyz";
        }

        private dynamic TestPropertyValue = default;
        public dynamic TestProperty { get { return TestPropertyValue; } set { TestPropertyValue = value; NotifyPropertyChanged(nameof(TestProperty)); } }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewmodels="clr-namespace:WpfApp1.ViewModels"  mc:Ignorable="d" DataContext="{DynamicResource ResourceKey=MainVM}">
    <Window.Resources>
        <viewmodels:MainViewModel x:Key="MainVM" />
    </Window.Resources>
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Path=TestProperty.Test}"/>
        <TextBlock Text="{Binding Path=TestProperty.ABC}"/>
    </StackPanel>
</Window>
issue-type-bug

Most helpful comment

@pocki: Great bug. Thanks for the report and minimal repro.

I've reproduced it locally and am investigating today. Thanks.

All 3 comments

@pocki: Great bug. Thanks for the report and minimal repro.

I've reproduced it locally and am investigating today. Thanks.

MS.Internal.SystemCoreHelper.IsIDynamicMetaObjectProvider is returning false for System.Dynamic.ExpandoObject which is causing the PropertyPath resolution to fail. The cause is a possible logic error in the AssemblyLoader preventing System.Core from being loaded.

Marking this as approved since we now understand this to be a bug. We really do need to fix this in P7.

/cc @ryalanms, @grubioe

Was this page helpful?
0 / 5 - 0 ratings