【问题标题】:Porting to WPF from WinForm, problems with the Data-Binding从 WinForm 移植到 WPF,数据绑定问题
【发布时间】:2012-02-10 11:54:08
【问题描述】:

我试图将 WinForms 项目移植到 WPF。但我在数据绑定方面遇到了一些问题。

我的 WinForms 程序有 DataGrid,它使用表单数据绑定组合框中的一列

His **Data** properties:
DataPropertyName **Developer**
DataSource **developerBindingSource1**
DataMember **DeveloperName**
ValueMember **Developer**

还有下一个代码

developerBindingSource1.DataSource = DT.Developer;
taskBindingSource.DataSource = DT.Task;
typeTaskBindingSource.DataSource = DT.TypeTask;

developBindObjBindingSource.DataSource = DevelopBindObj.GetBindingList(DT.Developer);
typeTaskBindObjBindingSource.DataSource = TypeTaskBindObj.GetBindingList(DT.TypeTask);

我使用额外的数据绑定类

class DevelopBindObj
{
    public Developer Developer { get; private set; }
    public string DeveloperName
    {
        get
        {
            return this.Developer.FIO;
        }
    }
        private DevelopBindObj(Developer Developer)
        {
            this.Developer = Developer;
        }           
    public static IBindingList GetBindingList(IEnumerable<Developer>Developers)        {BindingList<DevelopBindObj> result = new BindingList<DevelopBindObj>();
    foreach (var ee in Developers)
    {
        result.Add(new DevelopBindObj(ee));
    }
    return result;
}

我如何在 WPF 中做到这一点?

我试过了:

<DataGridComboBoxColumn x:Name="iDTypeTaskColumn"
                        Header="IDType Task"
                        Width="SizeToHeader"
                        SelectedValuePath="{Binding Source={StaticResource bindObjectsTaskViewSource}, Path=TypeTask}"
                        DisplayMemberPath="{Binding Source={StaticResource bindObjectsTaskViewSource}, Path=TypeTaskName}"
                        ItemsSource="{Binding Source={StaticResource bindObjectsTaskViewSource}}" SelectedItemBinding="{Binding Path=tblDevTypeTask}">

但这不起作用。

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    SelectedValuePathDisplayMemberPath 应该是 ComboBox 中项目的属性名称,而不是绑定

    <DataGridComboBoxColumn x:Name="iDTypeTaskColumn"
                            Header="IDType Task"
                            Width="SizeToHeader"
                            SelectedValuePath="TypeTask"
                            DisplayMemberPath="TypeTaskName"
                            ItemsSource="{Binding Source={StaticResource bindObjectsTaskViewSource}}" 
                            SelectedItemBinding="{Binding Path=tblDevTypeTask}">
    

    其中bindObjectsTaskViewSource 是一个对象集合,该集合中的每个对象都有一个名为TypeTaskTypeTaskName 的属性

    【讨论】:

      【解决方案2】:

      您的bindObjectsTaskViewSource 是否在资源中的任何位置声明,以便以这种方式访问​​... {StaticResource bindObjectsTaskViewSource}

      如果不是,那么您应该首先学习 WPF 的基础知识,以了解 SourceStaticResourceSourceDataContext 之间的区别。

      【讨论】:

        猜你喜欢
        • 2011-10-18
        • 2014-05-25
        • 1970-01-01
        • 2018-08-08
        • 1970-01-01
        相关资源
        最近更新 更多