我有以下XAML,效果很好:
UISplitViewController
DataGrid绑定到以下<DataGrid Name="dgList" ItemsSource="{Binding WeeklyTypes}" Grid.Row="0" Margin="0,0,0,0" ColumnWidth="1*"
CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True" CanUserReorderColumns="False" CanUserSortColumns="False">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Activity" Binding="{Binding Name}" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Monday, StringFormat={}{0:C2}}">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding WeekStart, StringFormat={}Monday {0:D2}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Tuesday" Binding="{Binding Tuesday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Wednesday" Binding="{Binding Wednesday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Thursday" Binding="{Binding Thursday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Friday" Binding="{Binding Friday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Total" Binding="{Binding Total, StringFormat={}{0:C2}}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding CashActivities}"
CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="All"
Width="{Binding ActualWidth, ElementName=dgList}" ColumnWidth="*" Margin="1,1,1,1" BorderThickness="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Activity" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Monday" Binding="{Binding Monday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Tueday" Binding="{Binding Tuesday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Wednesday" Binding="{Binding Wednesday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Thursday" Binding="{Binding Thursday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Friday" Binding="{Binding Friday, StringFormat={}{0:C2}}"/>
<DataGridTextColumn Header="Total" Binding="{Binding Total, StringFormat={}{0:C2}}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
类的集合:
WeeklyCashActivityType
除了我尝试将public class WeeklyCashActivityType
{
#region Variables
private CashActivityType _CashActivityType;
private List<WeeklyCashActivity> _CashActivities;
private decimal _Monday, _Tuesday, _Wednesday, _Thursday, _Friday, _Total;
private DateTime _WeekStart;
#endregion
#region Properties
public CashActivityType CashActivityType { get => _CashActivityType; }
public List<WeeklyCashActivity> CashActivities { get => _CashActivities; set => _CashActivities = value; }
public string Name { get => _CashActivityType.Type; }
public decimal Monday { get => _Monday; set => _Monday = value; }
public decimal Tuesday { get => _Tuesday; set => _Tuesday = value; }
public decimal Wednesday { get => _Wednesday; set => _Wednesday = value; }
public decimal Thursday { get => _Thursday; set => _Thursday = value; }
public decimal Friday { get => _Friday; set => _Friday = value; }
public decimal Total { get => _Total; set => _Total = value; }
public DateTime WeekStart { get => _WeekStart; set => _WeekStart = value; }
public DateTime TuesdayDate { get => _WeekStart.AddDays(1); }
public DateTime WednesdayDate { get => _WeekStart.AddDays(2); }
public DateTime ThursdayDate { get => _WeekStart.AddDays(3); }
public DateTime FridayDate { get => _WeekStart.AddDays(4); }
#endregion
#region Constructor
public WeeklyCashActivityType(CashActivityType type, DateTime startDate)
{
_CashActivityType = type;
_WeekStart = startDate;
_CashActivities = new List<WeeklyCashActivity>();
foreach(CashActivity activity in _CashActivityType.CashActivities)
{
_CashActivities.Add(new WeeklyCashActivity(this, activity));
}
Monday = _CashActivities.Sum(x => x.Monday);
Tuesday = _CashActivities.Sum(x => x.Tuesday);
Wednesday = _CashActivities.Sum(x => x.Wednesday);
Thursday = _CashActivities.Sum(x => x.Thursday);
Friday = _CashActivities.Sum(x => x.Friday);
Total = _CashActivities.Sum(x => x.Total);
}
#endregion
}
属性绑定到我的WeekStart
标头之外,所有这些都很好用。我确定我会丢失一些东西,但据我所知,我必须绑定到该类集合中。
我希望DataGrid标头显示如下:
活动| 2018年9月17日星期一| 2018年9月18日星期二| 2018年9月19日星期三