1

I am creating the binding in the code behind.

IEnumerable<IDictionary<string, object>> rows = dg1.ItemsSource.OfType<IDictionary<string, object>>();
        IEnumerable<string> columns = rows.SelectMany(d => d.Keys). Distinct(StringComparer.OrdinalIgnoreCase);
        foreach (string column in columns)
        {
            DataGridTemplateColumn col = new DataGridTemplateColumn();
             col.Header = column;

             // Create a factory. This will create the controls in each cell of this
             // column as needed.
             FrameworkElementFactory factory =
                 new FrameworkElementFactory(typeof(TextBox));

              **Binding b = new Binding() { Path=new PropertyPath(column)};**


             b.Mode = BindingMode.TwoWay;
             b.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
            // b.StringFormat = "d";
             b.Converter = new MyConverter();
             b.ConverterParameter = dg1;
             factory.SetValue(TextBox.TextProperty, b);
            // factory.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
             // Create the template itself, and add the factory to it.
             DataTemplate cellEditingTemplate = new DataTemplate();
             cellEditingTemplate.VisualTree = factory;

             col.CellTemplate = cellEditingTemplate;

             dg1.Columns.Add(col);

Now, if the value of the column starts with an opening ( and if it does not contain the closing braces ) then it gives the following error..

Syntax error in PropertyPath 'Unmatched parenthesis'

You can simply reproduce it like this:

public Window3()
{
        try
        {
            InitializeComponent();

            Binding b = new Binding() { Path = new PropertyPath("a(") };
        }
        catch (Exception)
        {

            throw;
        }
}

Moreover if the PropertyPath has the value like (abc) then also the binding fails .

What could be the solution to avoid such binding issues?

EDIT:

Any hint from this answer.

Community
  • 1
  • 1
Learner
  • 1,490
  • 2
  • 22
  • 35
  • 1
    I completely fail to understand your point.. What do you want that binding for? what kind of property is `a(`?? – Federico Berasategui Jan 16 '13 at 19:09
  • 1
    Its a dynamic UI where user can add the columns and they can add any string like abc( or (abc or anything but the binding breaks with such things. – Learner Jan 17 '13 at 01:43
  • But `column` is not a property its just a string, how is it going to resovle the property using the value of a string object – sa_ddam213 Jan 17 '13 at 03:31
  • When developers put together the words "Dynamic" and "UI", I immediately recognize they misunderstand or ignore the right approach (which is MVVM, DataTemplating and stuff like that) in WPF. WPF UIs are dynamic by nature, because of these aspects, and I dont see what's the point of creating a binding to an invalid property. – Federico Berasategui Jan 17 '13 at 04:32

0 Answers0