2013-10-06 23 views
0

在我的窗口中,我有一個簡單的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的測試這一點,我有接口實現,與出現的問題,並沒有它實現我想我會就這麼走了出來簡單起見。

任何幫助表示感謝你!

+0

當你有'INotifyPropertyChanged'代碼時,你的'dueDate'屬性是否還通知'monthYear'的額外事件? – Dan

+0

當我有我的INotifyPropertyChanged的代碼中,我用的dueDate設置爲monthyear二傳手,所以如果(_dueDate!=值),我會成立_dueDate,然後設置_monthYear在於dueDate二傳手內,並調用onPropertyChanged(「的dueDate」)和onPropertyChanged( 「monthYear」)在同一個setter中。 –

回答

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(); } 
    } 

現在,只要我排序我的列表集合,這是我想要的一致行爲,現在只需要重新應用分組。