DynamicObject - GetMemberBinder Name truncated after "."

5 posts, 1 answers
  1. Joel
    Joel avatar
    53 posts
    Member since:
    Sep 2013

    Posted 13 Sep Link to this post

    Hi,

      I have the following RadGridView where its ItemsSource which is bound to a collection that its item is inherited from DynamicObject.

      As shown in the screenshot, how can i prevent the column name with "." being truncated in TryGetMember() method?

    <telerik:RadGridView Grid.Row="1" Margin="4"
                         GroupRenderMode="Flat"
                         ShowGroupPanel="False"
                         RowIndicatorVisibility="Collapsed"
                         CanUserFreezeColumns="False"
                         ItemsSource="{Binding TestResultRecords, Mode=OneWay}"
                         AutoGenerateColumns="True"
                         IsReadOnly="True"
                         CanUserReorderColumns ="False"
                         CanUserInsertRows="False"
                         CanUserDeleteRows="False"
                         behavior:MyScrollIntoViewAsyncBehavior.IsEnabled="{Binding IsScrollToView, Mode=OneWay}">
                <telerik:RadGridView.SortDescriptors>
                    <telerik:SortDescriptor Member="{x:Static res:UIStringTable.DutCnt}"
                                            SortDirection="Ascending" />
                </telerik:RadGridView.SortDescriptors>
    </telerik:RadGridView>

    TestResultRecords = new ObservableCollection<VolatileDynamicColRecord>();
    m_TestRecord = new VolatileDynamicColRecord();
    m_TestRecord["Sweep Voltage1-@2.5mA(V)"] = 1234
    m_TestRecord["Sweep Voltage1-@2.8mA(V)"] = 5678
    m_TestRecord["Sweep Voltage1-Item3(V)"] = 2.74353
    TestResultRecords.Add(m_TestRecord);

    public class VolatileDynamicColRecord : DynamicObject, INotifyPropertyChanged
    {
        private readonly IDictionary<string, object> m_Data;
     
        public VolatileDynamicColRecord()
        {
            m_Data = new Dictionary<string, object>();
        }
     
        public VolatileDynamicColRecord(IDictionary<string, object> source)
        {
            m_Data = source;
        }
         
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return m_Data.Keys;
        }
     
        public IEnumerable<object> GetDynamicMemberValues()
        {
            return m_Data.Values;
        }
     
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            // Note: when column name contains ".", the text after "." will be truncated.
            result = this[binder.Name];
            return result != null ? true : false;
        }
     
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (this[binder.Name] == null)
            {
                return false;
            }
            this[binder.Name] = value;
            return true;
        }
     
        public object this[string columnName]
        {
            get
            {
                if (m_Data.ContainsKey(columnName))
                {
                    return m_Data[columnName];
                }
                return null;
            }
            set
            {
                if (!m_Data.ContainsKey(columnName))
                {
                    m_Data.Add(columnName, value);
                    OnPropertyChanged(columnName);
                }
                else
                {
                    if (m_Data[columnName] != value)
                    {
                        m_Data[columnName] = value;
                        OnPropertyChanged(columnName);
                    }
                }
            }
        }
     
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        #region INotifyPropertyChanged Members
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        #endregion

     

  2. Answer
    Martin Vatev
    Admin
    Martin Vatev avatar
    90 posts

    Posted 15 Sep Link to this post

    Hello Joel,

    The reason why the column names with '.'  are being truncated is because the binding path parser interprets them as full stop character. More information about paths and character escaping can be found in the Binding Declarations Overview. For your convenience, I prepared an example to demonstrate how to prevent this undesirable behavior. Please take a look at the implementation and consider how this approach fits your scenario.

    I hope that this helps. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Martin Vatev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Joel
    Joel avatar
    53 posts
    Member since:
    Sep 2013

    Posted 20 Sep in reply to Martin Vatev Link to this post

    Hi Martin,

    It works. Thanks.

  5. Joel
    Joel avatar
    53 posts
    Member since:
    Sep 2013

    Posted 21 Sep Link to this post

    Perhaps it is worth to mention that we can use (^) caret as escape character for comma (,) and other special characters.

    According to PropertyPath XAML Syntax: "Inside indexers ([ ]), the caret character (^) escapes the next character."

  6. Martin Vatev
    Admin
    Martin Vatev avatar
    90 posts

    Posted 23 Sep Link to this post

    Hello Joel,

    Thank you for sharing your solution with the community. We appreciate it.

    Regards,
    Martin Vatev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready