在我的窗口中,我有一個簡單的DataGrid控件,帶有一個dateTimePicker列和一個文本框列。我通過從dateTimePicker派生的月份字符串對datagrid中的行進行分組。這裏是XAML的列和分組.....WPF Datagrid與DateTimePicker控件分組的意外行爲
<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日期時間,爲此,我動態創建一個月,一年的字符串。下面是類的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的測試這一點,我有接口實現,與出現的問題,並沒有它實現我想我會就這麼走了出來簡單起見。
任何幫助表示感謝你!
當你有'INotifyPropertyChanged'代碼時,你的'dueDate'屬性是否還通知'monthYear'的額外事件? – Dan
當我有我的INotifyPropertyChanged的代碼中,我用的dueDate設置爲monthyear二傳手,所以如果(_dueDate!=值),我會成立_dueDate,然後設置_monthYear在於dueDate二傳手內,並調用onPropertyChanged(「的dueDate」)和onPropertyChanged( 「monthYear」)在同一個setter中。 –