0

I currently have a couple of Model objects as follows:

public class ModelBaseClass : INotifyPropertyChanged
{
    //Members omitted for brevity, but there is a DateTime property
    //that becomes important further down (see dummy method in application controller)      
}

and another:

public class ModelCollectionClass : INotifyPropertyChanged
{
    private ObservableCollection<ModelBaseClass> modelBaseClasses;
    public ObservableCollection<ModelBaseClass> ModelBaseClasses
    {
        get { return modelBaseClasses; }
        set
        {
            modelBaseClasses = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ModelBaseClasses");
        }
    }

    private void modelBaseClasses_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged(new PropertyChangedEventArgs("ModelBaseClasses"));
    }
}   

Then in one of my view's view-model, I have observable collections like such:

private ObservableCollection<ModelBaseClass> modelBaseClasses;
public ObservableCollection<ModelBaseClass> ModelBaseClasses
{
    get{ return modelBaseClasses ?? (modelBaseClasses = new ObservableCollection<ModelBaseClass>()); }
    set
    {
        modelBaseClasses = value;                
        OnPropertyChanged(new PropertyChangedEventArgs("ModelBaseClasses"));
    }
}    

private ObservableCollection<ModelCollectionClass> modelCollectionClasses;
public ObservableCollection<ModelCollectionClass> ModelCollectionClasses
{
        get { return modelCollectionClasses ?? (modelCollectionClasses = new ObservableCollection<ModelCollectionClass>()); }
        set
        {
            modelCollectionClasses = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ModelCollectionClasses"));                
        }
 }    

Then in my application controller (delegates work to view-models), I have a dummy method that loads up the ModelBaseClass collection like follows:

for (int i = 0; i < 8; i++)
{
    myViewModel.ModelBaseClasses.Add(new ModelBaseClass()
    {
       DateTimeProperty = Convert.ToDateTime("06/2" + i + "/2012 09:00:00 AM")
       //Other properties get set as well, using the i variable...
    }
}

It should be noted that there are more than one for loops in this method so that I can have plenty of data to work with.

Next, I have a method that takes that large collection of objects and groups them by date like follows:

myViewModel.ModelBaseClasses
                .GroupBy(modelBaseClass => ((DateTime)modelBaseClass.DateTimeProperty).Date)
                .Select(group => group.ToList())
                .ToList()
                .ForEach(
                    list => myiewModel.ModelBaseCollections.Add(new ModelBaseCollection() { ModelBaseClasses = new ObservableCollection<ModelBaseClass>(list) })
                );

Now this is where it starts getting more intricate. In the XAML of my view, I have an ItemsControl declared with its ItemsSource bound to the ModelCollectionClasses property in my view-model. It looks like this:

<ItemsControl ItemsSource="{Binding Path=DataContext.ModelCollectionClasses, RelativeSource={RelativeSource AncestorType={x:Type views:MyView}}}"
                              ItemTemplate="{StaticResource ItemsTemplate}">                    
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Width="{Binding ElementName=scroller, Path=ViewportWidth}"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

Then in a resource dictionary for this particular view, I have a DataTemplate declared like follows:

<DataTemplate x:Key="ItemsTemplate">
    <Grid DataContext="{Binding ModelBaseClasses}">            

        <DataGrid AutoGenerateColumns="False"
                  HeadersVisibility="None"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  ItemsSource="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}"
                  SelectedItem="{Binding Path=DataContext.SelectedModelBaseClass, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type views:MyView}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">                

            <DataGrid.Columns>                                          
                    ...Omitted for brevity...
            </DataGrid.Columns>
        </DataGrid>            
    </Grid>
</DataTemplate>

Now, with all of that said, I'm getting the desired visual representation of the data that I initially wanted. However, the issue I'm now running into is when I want to delete one of the ModelBaseClass objects. Because the ItemsSource of the DataGrid is being bound to the ModelBaseClasses property of the ModelCollectionClass, and not the ModelBaseClasses in my view-model, then when I remove a ModelBaseClass from the ModelCollectionClass found in my view-model I need to somehow notify the ModelCollectionClass of this change as well so that the UI will properly update.

I have seen a couple of other examples where some people have created wrapper classes to sync model collections and view-model collections, but I am having a tough time trying to implement such a thing with my own application. Any insight or helpful hints/ideas would be greatly appreciated.

4

1 に答える 1

0

率直に言って、あなたの質問は複雑すぎて、あなたが何をしたいのかよくわかりません。しかし、私はあなたのコードで 1 つのことに気付きました:プロパティModelCollectionClassを設定しModelBaseClassますが、設定したことを通知しますModelBaseClasses( es )。それが役立つかどうかはわかりません。

于 2012-06-26T17:43:16.873 に答える