我有简单的DataGrid
,有3列。两个是日期,第三个是只读列,用于计算日期之间的时间段。它工作正常,但Period
单元格仅在被选中后显示值。无法弄清楚原因。
这是我的DataGrid
<DataGrid ItemsSource="{Binding ClientList}" SelectedItem="{Binding SelectedClient, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" Margin="10,39,585,10">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Id}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Margin="585,39,10,10"
AutoGenerateColumns="False"
ItemsSource="{Binding PeriodTable}">
<DataGrid.Columns>
<DataGridTextColumn Header="Period"
Width="*"
IsReadOnly="True"
Binding="{Binding Path=Period, Mode=TwoWay, UpdateSourceTrigger=Default}"></DataGridTextColumn>
<DataGridTemplateColumn Header="DateFrom"
Width="*">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DateFrom, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DateFrom, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="DateTO" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DateTo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=DateTo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
和代码
private ObservableCollection<PeriodClass> _periodTable = new ObservableCollection<PeriodClass>();
public ObservableCollection<PeriodClass> PeriodTable
{
get { return _periodTable; }
set
{
if (_periodTable != value)
{
_periodTable = value;
RaisePropertyChanged("PeriodTable");
}
}
}
public class PeriodClass
{
private string _period;
private DateTime? _dateFrom;
private DateTime? _dateTo;
public string Period
{
get { return _period; }
set
{
if (_period != value)
{
_period = value;
}
}
}
public DateTime? DateFrom
{
get { return _dateFrom; }
set
{
if (_dateFrom != value)
{
_dateFrom = value;
}
}
}
public DateTime? DateTo
{
get { return _dateTo; }
set
{
if (_dateTo != value)
{
_dateTo = value;
if (DateTo > DateFrom)
{
int days = DateTo.Value.DayOfYear - DateFrom.Value.DayOfYear;
if ((days > 7) && (days < 14) && (days % 7) != 0)
{
if ((days % 7) > 1)
{
Period = "1 week and " + (days - 7).ToString() + "days";
}
else
{
Period = "1 week and " + (days - 7).ToString() + "day";
}
}
else
{
if ((days % 7) > 1)
{
Period = (days / 7).ToString() + " weeks and " + (days % 7) + " days";
}
else
{
Period = (days / 7).ToString() + " weeks and " + (days % 7) + " day";
}
}
if (days < 7)
{
Period = days.ToString() + " days";
}
if ((days % 7) == 0)
{
if ((days / 7) == 1)
{
Period = "1 week";
}
else
{
Period = (days / 7).ToString() + " weeks";
}
}
}
}
}
}
}