This is a migrated thread and some comments may be shown as answers.

DynamicObject - GetMemberBinder Name truncated after "."

4 Answers 202 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Iron
Joel asked on 13 Sep 2016, 09:46 AM

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

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 15 Sep 2016, 02:03 PM
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.
0
Joel
Top achievements
Rank 1
Iron
answered on 20 Sep 2016, 11:27 PM

Hi Martin,

It works. Thanks.

0
Joel
Top achievements
Rank 1
Iron
answered on 21 Sep 2016, 11:49 PM

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."

0
Martin
Telerik team
answered on 23 Sep 2016, 10:36 AM
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.
Tags
GridView
Asked by
Joel
Top achievements
Rank 1
Iron
Answers by
Martin
Telerik team
Joel
Top achievements
Rank 1
Iron
Share this question
or