我想用两级扩展器对数据进行分组,内部扩展器应该包含一个绑定到List的数据网格主要绑定类别:
public class BudgetProposalViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public BudgetProposal Proposal { get; set; }
public BudgetProposalViewModel(BudgetProposal proposal)
{
Proposal = proposal;
}
public ObservableCollection<PhaseCategory> Categories
{
get => new ObservableCollection<PhaseCategory>(Proposal.Phases);
set
{
Proposal.Phases = value.ToList();
OnPropertyChanged();
}
}
public class BudgetProposal
{
public string Name { get; set; }
public List<PhaseCategory> Phases { get; set; }
}
public class PhaseCategory
{
public string Name { get; set; }
public List<PhaseSubCategory> SubCategories { get; set; }
}
public class PhaseSubCategory
{
public string Name { get; set; }
public List<Task> Tasks { get; set; }
}
public class Task
{
public string Name { get; set; }
public DepartmentsValues DepartmentsContribution { get; set; }
public Task(string name, DepartmentsValues departmentsContribution)
{
Name = name;
DepartmentsContribution = departmentsContribution;
}
}
public class DepartmentsValues
{
public double Civil { get; set; }
public double Arch { get; set; }
public double Mech{ get; set; }
public DepartmentsValues(double civil, double arch, double mech)
{
Civil = civil;
Arch = arch;
Mech = mech;
}
}
这是我使用的测试对象
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext=new BudgetProposalViewModel(new BudgetProposal("Test Proposal",
new List<PhaseCategory>()
{
new PhaseCategory("Category 1",
new List<PhaseSubCategory>
{
new PhaseSubCategory("Sub Category 1",
new List<Task>
{
new Task("Task 1",new DepartmentsValues(1,2,3)),
new Task("Task 2",new DepartmentsValues(1,2,3)
)
}),
new PhaseSubCategory("Sub Category 2",
new List<Task>
{
new Task("Task 3",new DepartmentsValues(1,2,3)
)
})
}),
new PhaseCategory("Category 2",
new List<PhaseSubCategory>
{
new PhaseSubCategory("Sub Category 3",
new List<Task>
{
new Task("Task 4",new DepartmentsValues(1,2,3)
)
})
}),
}){});
}
}
我尝试在XAML中执行以下操作,但内部扩展器没有显示数据
<Grid d:DataContext="{d:DesignData BudgetProposalViewModel}">
<Grid.Resources>
<CollectionViewSource x:Key="groups" Source="{Binding Categories}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<DataGrid x:Name="GridProposalCategories" ItemsSource="{Binding Source={StaticResource groups}}" AutoGenerateColumns="False">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}" BasedOn="{StaticResource MahApps.Styles.GroupItem.DataGrid}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" d:DataContext="{d:DesignData PhaseCategory}">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Name}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<Expander IsExpanded="True" d:DataContext="{d:DesignData PhaseSubCategory}">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Items[0].Name}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<DataGrid ItemsSource="{Binding Path=Items[0].Tasks}" AutoGenerateColumns="True"></DataGrid>
</Expander.Content>
</Expander>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>
我的数据绑定出了什么问题?输出栅极
内部展开不显示数据,因为您对设计DataContext做出了错误的假设。它不是PhaseSubCategory,它仍然是PhaseCategory。将最后一个DataGrid更改为Items[0].SubCategories
而不是Items[0].Tasks
,它就会显示。
<Expander.Content>
<Expander IsExpanded="True" d:DataContext="{d:DesignData PhaseSubCategory}">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Items[0].Name}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<DataGrid ItemsSource="{Binding Path=Items[0].SubCategories}" AutoGenerateColumns="True"></DataGrid>
</Expander.Content>
</Expander>
</Expander.Content>