1

我正在后面的代码中创建绑定。

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);

现在,如果列的值以一个开头(,并且如果它不包含右大括号),那么它会给出以下错误..

PropertyPath 中的语法错误“括号不匹配”

您可以像这样简单地复制它:

public Window3()
{
        try
        {
            InitializeComponent();

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

            throw;
        }
}

此外,如果PropertyPath具有类似(abc)then 的值,则绑定也会失败。

避免此类绑定问题的解决方案是什么?

编辑:

这个答案的任何提示。

4

0 回答 0