2
votes

I'm developing an application using the WAF (WPF Application Framework) which is based on MVVM, MEF, etc.

I currently have a couple of Domain objects with similar structures as below (shortened for brevity):

public class MyBaseClass : INotifyPropertyChanged
{
    private DateTime? _firstDateTime;
    protected DateTime? firstDateTime
    {
        get { return _firstDateTime; }
        set
        {
            _firstDateTime = value;
            OnPropertyChanged(new PropertyChangedEventArgs("FirstDateTime"));
    }

    private DateTime? _secondDateTime;
    protected DateTime? secondDateTime
    {
        get { return _secondDateTime; }
        set
        {
            _secondDateTime = value;
            OnPropertyChanged(new PropertyChangedEventArgs("SecondDateTime"));
    }

    public DateTime? FirstDateTime
    {
        get { return firstDateTime; }
        set { firstDateTime = value; }
    }

    public DateTime? SecondDateTime
    {
        get { return secondDateTime; }
        set { secondDateTime = value; }
    }
}

public class MyBaseCollectionClass : INotifyPropertyChanged
{
    private ObservableCollection<MyBaseClass> _baseClassObjectCollection;
    protected ObservableCollection<MyBaseClass> baseClassObjectCollection
    {
        get { return _baseClassObjectCollection; }
        set
        {
            _baseClassObjectCollection = value;

            OnPropertyChanged(new PropertyChangedEventArgs("BaseClassObjectCollection"));
        }
    }        

    public ObservableCollection<MyBaseClass> BaseClassObjectCollection
    {
        get { return baseClassObjectCollection; }
        set { baseClassObjectCollection = value; }
    }
}

Then, in my ApplicationController I have a method that loads up an ObservableCollection in a view-model like such:

for (int i = 0; i <= 9; i++)
{
    detailsViewModel.MyBaseClassObjects.Add( new MyBaseClass()
                                            {
                                                FirstDateTime = Convert.ToDateTime("05/2" + i + "/2012 09:00:00 AM"),
                                                SecondDateTime = Convert.ToDateTime("05/2" + i + "/2012 04:30:00 PM")
                                            });
}

And another method that groups the big collection into smaller grouped collections by date:

detailsViewModel.MyBaseClassObjects
                .GroupBy(baseObject => ((DateTime)baseObject.FirstDateTime).Date)
                .Select(group => group.ToList())
                .ToList()
                .ForEach(list => detailsViewModel.BaseObjectCollections
                        .Add(
                              new MyBaseCollectionClass()
                              { BaseClassObjectCollection = new ObservableCollection<MyBaseClass>(list)}
                             )
                         );

Then, in my view's XAML, I have an ItemsControl bound like such:

<ItemsControl ItemsSource="{Binding BaseObjectCollections}" ItemTemplate="{StaticResource ItemsTemplate}">                    
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

An in a ResourceDictionary for the view, I have a DataTemplate declared like such (some items omitted for clarity):

<DataTemplate x:Key="ItemsTemplate">
    <Grid DataContext="{Binding MyBaseClassObjects}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>            

        <DataGrid Grid.Row="1"
                      Background="Transparent"
                      Style="{DynamicResource DataGridStyle}"
                      AlternatingRowBackground="Gainsboro"
                      AlternationCount="2"
                      AutoGenerateColumns="False"
                      HeadersVisibility="None"
                      ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext}">
            <DataGrid.Columns>
                <DataGridTextColumn Width=".25*">
                    <DataGridTextColumn.Binding>
                        <MultiBinding Converter="{x:Static myConvNS:MultiValueTESTCONVERTER.Retrieve}">
                            <Binding Path="FirstDateTime"/>
                            <Binding Path="SecondDateTime"/>
                        </MultiBinding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>                    
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</DataTemplate>

NOTE: In the above DataTemplate I have a multibinding currently so I can see what values are being passed into the converter. I realize I will not be able to achieve the desired effect (described below), but I wanted to be able to step through the binding and look at what the converter was receiving - nothing more at this point. Ultimately what I'm trying to accomplish (if possible) is that I'd like to be able to bind my Datagrid in such a way that the FirstDateTime and SecondDateTime values can be displayed in the same column, alternating. See the image below for a visual aid:

Desired Effect

My initial thoughts were to have some other generic object that exposes a single DateTime property and break down my base objects (which have two DateTime properties) into instances of this generic object, then bind to a collection of those generic objects. However, I don't like this idea. When a change occurs to either DateTime properties on my base object I need to know about it and I'm afraid that by splitting this object into two generic objects that I will lose the ability to send/receive the notifications correctly.

Anyone have any suggestions or thoughts?

1

1 Answers

0
votes

what about a DataGridTemplateColumn and just use a StackPanel with your two Textboxes?

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBox Text="{Binding FirstDateTime}"/>
                <TextBox Text="{Binding SecondDateTime}"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>