【问题标题】:DataBinding in a List View列表视图中的数据绑定
【发布时间】:2009-03-13 18:13:34
【问题描述】:

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

我有一个列表视图,我为其设置了一个数据源。 DataSource 是“CatalogPartRows”的数组列表。我在代码中创建我的列。然后我设置他们的单元格模板(我的一些列包含组合框和复选框)。我的问题是我需要在“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

谢谢, 拉吉。

【问题讨论】:

    标签: wpf data-binding xaml objectdataprovider


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2012-02-10
      相关资源
      最近更新 更多