多绑定XamDataGrid

时间:2015-03-10 19:39:17

标签: c# wpf xaml infragistics

我正在尝试使用以下code example from the Infragistics site,我希望XamDataCards中的编辑能够反映在XamDataGrid中。但是,我的ViewModel中的XamDataGrid的DataSource是ObservableCollection<Companies>。我怎样才能绑定到卡并将更新中继回ViewModel中的Companies对象?

<igDP:XamDataGrid x:Name="dgCompanies" Theme="IGTheme" DataSource="{Binding Companies}" SelectedDataItemsScope="RecordsOnly">
  <igDP:XamDataGrid.FieldSettings>
    <igDP:FieldSettings CellClickAction="SelectCell" AllowEdit="True"/>
  </igDP:XamDataGrid.FieldSettings>
</igDP:XamDataGrid>
<igDP:XamDataCards x:Name="XamDataCards1"
                       Grid.Row="1"
                       DataSource="{Binding Path=SelectedDataItems, ElementName=dgCompanies}"
                       Theme="IGTheme">

修改:添加了ViewModel

public class CompanyMgmtViewModel : ViewModelBase
{
    private ObservableCollection<Object> _Companies = null;

    public ObservableCollection<Object> Companies
    {
        get { return _Companies; }
        set
        {
            if (_Companies != value)
            {
                _Companies = value;
                RaisePropertyChanged(GetPropertyName(() => Companies));
            }
        }
    }
    public CompanyMgmtViewModel()
    {
        this.LoadData();
    }

    public void LoadData()
    {
        ObservableCollection<Object> records = new ObservableCollection<Object>();

        var results = from res in AODB.Context.TCompanies
                      select res;
        foreach (var item in results)
            if (item != null) records.Add(item);
        Companies = records;
    }
}

Model / Context代码只是EF Database First生成的。

2 个答案:

答案 0 :(得分:1)

您需要将XamDataGrid的SelectedDataItems属性绑定到object []类型的属性ie。 ViewModel中的SelectedCompanies并绑定到XamDataCards数据源的公司。

此线程中接受的答案有一个示例,说明如何执行此操作,尽管使用ListBox而不是XamDataCards: http://www.infragistics.com/community/forums/t/89122.aspx

只需用您的XamDataCards控件替换ListBox,它就可以工作并更新XamDataGrid。示例中的ViewModel包含在MainWindow代码隐藏中,因此它是您想要的MVVM。

更多信息: http://help.infragistics.com/Help/Doc/WPF/2014.1/CLR4.0/html/xamDataGrid_Selected_Data_Items.html

IG的SelectedDataItems是一个对象[]: http://help.infragistics.com/Help/Doc/WPF/2014.1/CLR4.0/html/InfragisticsWPF4.DataPresenter.v14.1~Infragistics.Windows.DataPresenter.DataPresenterBase~SelectedDataItems.html

答案 1 :(得分:0)

如果没有Theodosius和Ganesh的意见,我无法得到这个答案 - 所以多亏了他们,他们都得到了部分答案。

我首先尝试通过ViewModel上的属性将XamDataGrid的SelectedDataItems绑定到XamDataCards,如Theodosius建议的那样,但这还不够。感谢Ganesh,我在我的模型对象上实现了INotifyPropertyChanged,继承自MVVMLight中的ObservableObject(我怎么不知道Model需要这个?)。

以下是使其有效的相关代码段。 我还实施了PropertyChanged.Fody as documented here;这就是TypedViewModelBase<T>RaisePropertyChanged()的删除来自的地方。 我也使用LINQ / Automapper .Project().To<T>()调用which can be found here来创建我的Model对象。

<强>模型

 public class Company : ObservableObject
{
    public Company() { }
    public int id { get; set; }
    public string strName { get; set; }
    public string strDomicileCode { get; set; }
}

<强>视图模型

public class CompanyMgmtViewModel : TypedViewModelBase<Company>
{
    private ObservableCollection<Object> _Companies = null;
    private Object[] _selectedCompany = null;

    public Object[] Company
    {
        get { return _selectedCompany; }
        set
        {
            if (_Company != value)
            {
                _selectedCompany = value;
            }
        }
    }

    public ObservableCollection<Object> Companies
    {
        get { return _Companies; }
        set
        {
            if (_Companies != value)
            {
                _Companies = value;
            }
        }
    }
    public CompanyMgmtViewModel()
    {
        this.LoadData();
    }

    public void LoadData()
    {
        ObservableCollection<Object> records = new ObservableCollection<Object>();

        var results = AODB.Context.TCompanies.Project().To<Company>();
        foreach (var item in results)
            if (item != null) records.Add(item);
        Companies = records;
    }
}

查看

<igDP:XamDataGrid   x:Name="dgCompanies" 
                Theme="IGTheme"  
                DataSource="{Binding Companies, Mode=OneWay}" 
                SelectedDataItemsScope="RecordsOnly" 
                SelectedDataItems="{Binding Company}">
                  ...
<igDP:XamDataCards x:Name="XamDataCards1"
                       Grid.Row="1"
                       DataSource="{Binding ElementName=dgCompanies, Path=SelectedDataItems}"
                       Theme="IGTheme">