带有括号的PropertyPath与绑定

时间:2013-01-16 15:58:46

标签: c# wpf binding

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

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),那么绑定也会失败。

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

修改

来自this的任何提示。

0 个答案:

没有答案