Datagrid列绑定

时间:2012-06-19 13:50:27

标签: c# wpf xaml binding datagrid

我正在使用基于MVVM,MEF等的WAF(WPF应用程序框架)开发应用程序。

我目前有几个Domain对象具有如下结构(为简洁起见缩短):

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值可以在同一列中交替显示。请参阅下图以获取视觉帮助:

Desired Effect

我最初的想法是让一些其他通用对象暴露单个DateTime属性,并将我的基础对象(具有两个DateTime属性)分解为此通用对象的实例,然后绑定到这些通用对象的集合。但是,我不喜欢这个想法。当我的基础对象上的任何DateTime属性发生更改时,我需要了解它,我担心通过将此对象拆分为两个通用对象,我将无法正确发送/接收通知。

有人有任何建议或想法吗?

1 个答案:

答案 0 :(得分:0)

DataGridTemplateColumn怎么样,只需将StackPanel与两个Textbox一起使用?

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