views:

341

answers:

2

Pretty simple task, but the source code doesn't do required job... Please advise.

There is Products collection in the class (approach is based on the MVVm pattern, but that is not influe on the current issue):

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

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

Here is class declaration:

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
}

The class instance is binded to the window's Grid data context:

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

And here is xaml code of the window:

<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>

The xamDataGrid sampe is the same. overall code is pretty simple, but doesn't work.

Does anybody know why? Any thoughts are welcome.

How could I debug binding to resolve the problem himselft?

Thanks.

+1  A: 

Ok, maybe this won't exactly answer your question, but it looks like you are instantiating your viewmodel class twice. Once in your code, immediately after creating your window, and once in the ObjectDataProvider thing. It will probably be easier to debug if you settle on one of them. Suggestion:
1. Comment out this line: wnd.MainGrid.DataContext = new ProductWindowViewModel();
2. Set a breakpoint inside the constructor of your viewmodel 3. Start it up and see if the breakpoint gets hit. If it does get hit, you know you are doing something right.

Also, check the output window in visual studio and see if any binding exceptions are reported there.

Henrik Söderlund
Thanks, I guess you right. At the moment I've already resolved issue, solution is in another comment.
Budda
A: 

In my case the code in the windows constructor is redundant. Now all is working, seems like removing unnecessary assignement resolved an issue.

Budda