locked
Binding Issues when using reflect? RRS feed

  • Question

  • Ok this is a little complex.

    Without going into it too much, I'm reflecting over a class to collect it's fields and then adding each field into a class

        public class MBINField
        {
            public string Name { get; set; }
            public dynamic Value { get; set; }
            public string NMSType { get; set; }
        }

    I'm then adding all of these to a collection, and binding that collection to a listview itemsource, from there i use a TemplateSelector to display a datatemplate based on the type of object that is stored.

    Now my issue comes from when the Value of an MBINField is a class, for some reason I cannot bind to the properties of that class. It throws the follow error:

    System.Windows.Data Error: 40 :
    BindingExpression path error: 'Name' property not found on 'object' ''GcBaseBuildingPalette' (HashCode=1107452)'. 
    BindingExpression:Path=Name; 
    DataItem='GcBaseBuildingPalette' (HashCode=1107452); 
    target element is 'TextBox' (Name=''); 
    target property is 'Text' (type 'String')

    System.Windows.Data Information: 20 : 
    BindingExpression cannot retrieve value due to missing information. 
    BindingExpression:Path=Name; DataItem='GcBaseBuildingPalette' (HashCode=1107452); 
    target element is 'TextBox' (Name=''); 
    target property is 'Text' (type 'String')

    System.Windows.Data Information: 21 : 
    BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. 
    BindingExpression:Path=Name; 
    DataItem='GcBaseBuildingPalette' (HashCode=1107452); 
    target element is 'TextBox' (Name=''); 
    target property is 'Text' (type 'String')

    System.Windows.Data Information: 10 : 
    Cannot retrieve value using the binding and no valid fallback value exists; 
    using default instead.
    BindingExpression:Path=Name; DataItem='GcBaseBuildingPalette' (HashCode=1107452); 
    target element is 'TextBox' (Name=''); 
    target property is 'Text' (type 'String')

    Now, i can confirm through debug that GcBaseBuildingPalette does indeed have a property called Name, and that the datacontext is correct and have a valid GcBaseBuildingPalette object. In fact if i add a onclick event to the textbox and in code behind set the value, it works without an issue

    TextBox tb = (sender as TextBox);
    MBINField field = (tb.DataContext as MBINField);
    libMBIN.Models.Structs.GcBaseBuildingPalette  c = field.Value as libMBIN.Models.Structs.GcBaseBuildingPalette;
    tb.Text = c.Name;

    this works without any issues, So i cannot for the life of me work out why it wont work, it's also possible of interest that adding a converter to this problem never gets called, breakpoints are never hit in either Convert or ConvertBack.

    I'm hoping this is something that is easy to fix!


    Ben Murray

    Saturday, August 25, 2018 2:58 PM

Answers

  • Hi Cherry,

    Thanks for the reply. Unfortunatly i found the issue. I had overlooked the fact that the GcBaseBuildingPalette dosn't actually have a property, it has a public field. 

    Appartently it's all important to have {get; set} tagged on the end or it wont work. Unfortunaltly that code is from a library that needs to keep them as public fields, so we needed to find another solution.

    Thanks


    Ben Murray

    • Marked as answer by Wannabeuk Wednesday, August 29, 2018 9:14 AM
    Monday, August 27, 2018 8:15 AM

All replies

  • Hi Ben,

    According to your description, I just see the MBINField and some error here, please provide some xaml and xaml.cs here, I will try your code and try to reproduce your issue here.

    Best Rgeards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 27, 2018 5:38 AM
  • Hi Cherry,

    Thanks for the reply. Unfortunatly i found the issue. I had overlooked the fact that the GcBaseBuildingPalette dosn't actually have a property, it has a public field. 

    Appartently it's all important to have {get; set} tagged on the end or it wont work. Unfortunaltly that code is from a library that needs to keep them as public fields, so we needed to find another solution.

    Thanks


    Ben Murray

    • Marked as answer by Wannabeuk Wednesday, August 29, 2018 9:14 AM
    Monday, August 27, 2018 8:15 AM
  • Hi Wannabeuk,

    So you resolved your issue now? If yes, please remember to close your thread by marking your reply as answer or sharing your solution here, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 29, 2018 9:07 AM