我正在開發使用了基於MVVM,MEF的WAF(WPF應用程序框架)的應用程序,等等DataGrid列結合
我現在有一對夫婦域的與如下面類似結構的對象(縮短簡潔):
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; }
}
}
然後,在我的ApplicationController我有一個加載了像這樣的視圖模型一個ObservableCollection的方法:
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")
});
}
這組大集合分成更小的分組集合按日期另一種方法:
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)}
)
);
然後,在我看來的XAML,我有一個ItemsControl綁定像這樣:
<ItemsControl ItemsSource="{Binding BaseObjectCollections}" ItemTemplate="{StaticResource ItemsTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
在一個ResourceDictionary中,我有一個DataTemplate類似的聲明(爲了清楚起見省略了一些項目):
<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>
注意:在上面的DataTemplate中,我有一個多重綁定,所以我可以看到什麼值被傳遞到轉換器。我意識到我無法達到預期的效果(如下所述),但我希望能夠逐步完成綁定,並查看轉換器正在接收什麼 - 在這一點上沒有更多。 最終我試圖完成(如果可能的話)是我希望能夠以這樣的方式綁定我的Datagrid,使FirstDateTime和SecondDateTime值可以交替顯示在同一列中。請參閱下面的圖像的直觀教具:
我最初的想法是有一個公開的單一的DateTime屬性其他一些通用的對象,我的基本對象(其中有兩個日期時間屬性)分解成的情況下,這個通用對象,然後綁定到這些通用對象的集合。但是,我不喜歡這個想法。當我的基礎對象上的任何DateTime屬性發生更改時,我需要知道它,並且恐怕通過將此對象拆分爲兩個通用對象,我將失去正確發送/接收通知的能力。
任何人有任何建議或想法?
對不起,直到現在沒有迴應,但我會給這個鏡頭,看看我得到什麼樣的結果。謝謝! – dgsmith15