DataGrid的ItemsSource绑定到ObservableCollection。两个DataGridColumns绑定到集合中的DateTime属性,而第三列用于使用IMultiValue Converter显示它们之间的日期差异。我尝试理解和解决的行为可以在场景2中找到。
场景1:没有问题
打开视图,DataGrid已包含记录,因为该集合不为空。如果将新对象添加到集合中,它将显示在DataGrid中,最后一列将正确显示日期差异值。
场景2:有问题
视图已打开,但DataGrid不包含任何记录,因为该集合为空。如果将新对象添加到集合中,它将显示在DataGrid中,但最后一列(包含转换器)为空。但是,如果视图随后关闭并重新打开,则日期差异在DataGrid中显示正确。
我希望在首次将对象添加到空集合时,在DataGrid列中显示日期差异值。我错过了什么?
对象类
df.rename_axis('Player').reset_index().melt('Player').dropna()
Player variable value
2 Asghar Stanikzai Afghanistan 809.0
10 Mohammad Nabi Afghanistan 851.0
11 Mohammad Shahzad Afghanistan 1713.0
12 Najibullah Zadran Afghanistan 643.0
14 Samiullah Shenwari Afghanistan 774.0
16 AJ Finch Australia 1082.0
18 CL White Australia 988.0
19 DA Warner Australia 1691.0
21 GJ Maxwell Australia 822.0
28 SR Watson Australia 1465.0
30 AD Hales England 1340.0
35 EJG Morgan England 1577.0
37 JC Buttler England 985.0
38 KP Pietersen England 1176.0
39 LJ Wright England 759.0
视图模型
public class Evaluation
{
public int ID { get; set; }
public DateTime BirthDate { get; set; }
public DateTime TestDate { get; set; }
}
查看
public class EvaluationViewModel : ViewModelBase
{
private ObservableCollection<Evaluation> evaluations;
public class EvaluationViewModel()
{
evaluations = Utility.Convert<Evaluation>(db.evaluationRepository.GetAllById(Subject.ID));
TestView = (CollectionView)new CollectionViewSource { Source = Evaluations }.View;
TestView.SortDescriptions.Add(new SortDescription("TestDate", ListSortDirection.Ascending));
}
public ObservableCollection<Evaluation> Evaluations
{
get { return evaluations; }
}
public CollectionView TestView { get; set; }
}
转换器
public class Evaluation
{
public int ID { get; set; }
public DateTime BirthDate { get; set; }
public DateTime TestDate { get; set; }
}
<Window.Resources>
<converters:DateDiffMonthMultiConverter x:Key="DateConverter"/>
</Window.Resources>
<DataGrid ItemsSource="{Binding TestView}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" Visibility="Hidden"/>
<DataGridTextColumn Header="Birth Date" Binding="{Binding BirthDate}"/>
<DataGridTextColumn Header="Test Date" Binding="{Binding TestDate}"/>
<DataGridTemplateColumn Header="Age When Tested">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource DateConverter}">
<Binding Path="BirthDate"/>
<Binding Path="TestDate"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:0)
我尝试了你的代码(2秒后向ObservableCollection添加一个项目),它为我工作。这是我的代码:
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
DataContext = new EvaluationViewModel();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() => Thread.Sleep(2000))
.ContinueWith((t) =>
{
(DataContext as EvaluationViewModel).Evaluations.Add(
new Evaluation() { ID = 2, BirthDate = DateTime.Now.AddYears(-22), TestDate = DateTime.Now });
}, TaskScheduler.FromCurrentSynchronizationContext());
}
视图模型
public EvaluationViewModel()
{
Evaluations = new ObservableCollection<Evaluation>();
TestView = (CollectionView)new CollectionViewSource { Source = Evaluations }.View;
TestView.SortDescriptions.Add(new SortDescription("TestDate", ListSortDirection.Ascending));
}
public ObservableCollection<Evaluation> Evaluations { get; }
public CollectionView TestView { get; set; }
答案 1 :(得分:0)
事实证明转换器不是问题,但是转换器使用的值之一是DependencyProperty(我的错误直到现在才识别它)并且抛出了DependencyProperty.UnsetValue错误。我在向集合添加新实体时使用 CreateNew()方法解决了这个问题,因此在将对象加载到DataGrid时已知导航属性。