列表视图中的DataBinding

时间:2009-03-13 18:13:34

标签: wpf data-binding xaml objectdataprovider

是XAML / WPF的新手,遇到过这个奇怪的问题:

我有一个列表视图,我设置了一个DataSource。 DataSource是“CatalogPartRows”的arraylist。我在代码中创建了我的列。然后我设置他们的单元格模板(我的一些列包含组合框和复选框)。我的问题是我需要在“CatalogPartRow”类中调用一个函数来获取我需要在单元格中设置的字符串。

以下是我尝试使用的代码:

            // THIS DOES NOT WORK
            //
            ObjectDataProvider ODP = new ObjectDataProvider();
            ODP.MethodName = "PropertyValueAsString";
            ODP.MethodParameters.Add(PropertyName);
            ODP.ObjectType = typeof(CatalogPartRow);

            Binding DataBindingText = new Binding();
            DataBindingText.Source = ODP;

            // THIS WORKS
            //
            //String BindingPathText = /*NOXLATE*/"PropertyValues[" + CPR.IndexOf(PropertyName) + /*NOXLATE*/"]";
            //Binding DataBindingText = new Binding(BindingPathText);

            FrameworkElementFactory TextBlockElement = new FrameworkElementFactory(typeof(TextBlock));
            TextBlockElement.SetBinding(TextBlock.TextProperty, DataBindingText);

            FrameworkElementFactory PropertyColumnElement = new FrameworkElementFactory(typeof(Grid));
            PropertyColumnElement.AppendChild(TextBlockElement);

            DataTemplate DT = new DataTemplate();
            DT.VisualTree = PropertyColumnElement;

            GVC.CellTemplate = DT;

我的方法是否正确?

CPR = CatalogPartRow

GVC = GridViewColumn

谢谢, 拉吉。

1 个答案:

答案 0 :(得分:0)

由于将方法包装在属性中将不起作用(因为需要参数),最好的宠物可能是创建一个值转换器。您可以使用PropertyName作为列的绑定,然后在转换器中将其传递给方法,并返回方法的值。