2010-01-22 114 views
0

非常簡單的任務,但源代碼不需要工作...請指教。如何將對象列表綁定到infragistics數據網格?

有產品集合中的類(方法是基於MVVM模式,但沒有對當前問題influe):

public class ProductWindowViewModel : WorkspaceViewModel // implements INotifyPropertyChanged 
{ 
    public ProductWindowViewModel() 
    { 
     Products = new List<Product>(ProductService.Instance.Repository.GetAll()); 
    } 

    List<Product> Products { get; set; } 
} 

這裏是類的聲明:

public class Product : IEntity 
{ 
    #region Public Properties 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int Cost { get; set; } 

    #endregion 
} 

類實例綁定到窗口的Grid數據上下文:

ProductWindow wnd = new ProductWindow(); 
wnd.MainGrid.DataContext = new ProductWindowViewModel(); 
wnd.ShowDialog(); 

這裏是xaml cod窗口的E:

<Window x:Class="WpfTest1.ProductWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="ProductWindow" Height="300" Width="300" xmlns:igDP="http://infragistics.com/DataPresenter" 
xmlns:ViewModel="clr-namespace:ViewModel;assembly=ViewModel"> 

<Grid x:Name="MainGrid"> 
    <Grid.Resources> 
     <ObjectDataProvider x:Key="odpObjectDataProvider" ObjectType="{x:Type ViewModel:ProductWindowViewModel}" /> 
    </Grid.Resources> 
    <Grid DataContext="{StaticResource odpObjectDataProvider}"> 
     <igDP:XamDataGrid DataSource="{Binding Path=Products}"/> 
    </Grid> 
</Grid> 

的xamDataGrid SAMPE是相同的。整體代碼非常簡單,但不起作用。

有人知道爲什麼嗎?任何想法都歡迎。

我該如何調試綁定來解決問題呢?

謝謝。

回答

0

在我的情況下,Windows構造函數中的代碼是多餘的。現在一切正常,看起來像刪除不必要的分配解決了一個問題。

1

好吧,也許這不會完全回答你的問題,但它看起來像你實例化你的viewmodel類兩次。一旦在你的代碼中,在創建你的窗口之後,立刻在ObjectDataProvider事物中。如果你解決其中一個問題,它可能會更容易調試。建議:
1.註釋掉這一行:wnd.MainGrid.DataContext = new ProductWindowViewModel();
2.將您的視圖模型 3.啓動它的構造函數中的斷點,看看斷點被擊中。如果它被擊中,你知道你正在做的事情是正確的。

此外,檢查Visual Studio中的輸出窗口,看看是否有任何綁定異常報告。

+0

謝謝,我想你是對的。目前我已經解決了問題,解決方案還有另一條評論。 – Budda

相關問題