使用DateTimePicker控件对WPF Datagrid进行分组时出现意外行为

时间:2013-10-06 18:55:32

标签: wpf datagrid wpfdatagrid datetimepicker

在我的窗口中,我有一个带有dateTimePicker列的简单数据网格控件和一个文本框列。我将数据网格中的行分组为从dateTimePicker派生的月 - 年字符串。这是列和分组的xaml .....

                <DataGridTemplateColumn Header="Date" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <xctk:DateTimePicker IsEnabled="True" Format="Custom" FormatString="M/d/yyyy h:mm" Value="{Binding theDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Text" Width="150">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox TextWrapping="Wrap" AcceptsReturn="True" MaxLines="2" VerticalScrollBarVisibility="Auto" MaxLength="150" Text="{Binding theText, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}" FontWeight="Bold" Padding="3"/>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </DataGrid.GroupStyle>

我有一个非常简单的类,包含文本字符串和datetimepicker datetime,以及我动态创建的月份字符串。这是该类的C#代码......

public DateTime dueDate { get; set; }
public String task { get; set; }

private String _monthYear;
public String monthYear
{
    get { return dueDate.Month.ToString() + " - " + dueDate.Year.ToString(); }
}

这是我对分组的初始化

myCollectionView = CollectionViewSource.GetDefaultView(myObservableCollectionList);
myCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("monthYear"));

MyDataGrid.ItemsSource = myCollectionView;
// In this case, myObservableCollectionList is an observable collection that holds the datagrid 
// rows. myCollectionView is an ICollectionView object

除了一个非常烦人的意外行为外,一切都正常。我不知道这是否是来自扩展WPF工具包的DateTimePicker控件的错误。

每当我在DateTimePicker控件上更改月份或年份并单击同一行中的新行或不同列时,我更改日期的行将不会被分组但将保留在旧月份日期内小组,直到我排序。如果它是一致的,那么这种行为会很好......

如果我要在DateTimePicker控件上更改月份或年份,然后选项卡或单击相同的DateTimePicker控件(不更改任何内容),然后单击新行,则旧行将分组为新的月份类别。

我不确定,但我觉得这是一个DateTimePicker控件的错误,即使你没有更改日期,当你选中控件时它会调用某种事件。但是现在我很困惑,并且想知道是否有人知道为什么会发生这种情况。

* 注意 ** 我在使用和不使用INotifyPropertyChanged的情况下对此进行了测试,我确实已经实现了接口,并且无论是否实现都会出现行为我想我为了简单起见,我会把它留下来。

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,它是由_monthYear和monthYear的使用引起的,所以我想每当我改变日期时,就会有多次调用来改变_monthYear。无论出于何种原因,我都假设与小组描述有关...

taskScheduleCollection.GroupDescriptions.Add(new PropertyGroupDescription("monthYear"));

可能与ValueChanged或DateTimePicker中的类似事件混在一起会导致分组重新应用,并且会创建新的行组(我对此不是100%肯定)。

我通过删除_monthYear属性来修复此问题,因为它是不必要的,只保留一个依赖于日期的monthYear属性。

    public String monthYear
    {
        get { return _date.Month.ToString() + " - " + _date.Year.ToString(); }
    }

分组现在只在我对列表集合进行排序时才重新应用,这是我想要的一致行为。