datagrid里数据始终不显示,但是树节点数据绑定后显示是正常的,前台代码如下
<TreeView x:Name="departmentTree" Margin="0,0,259,20">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<StackPanel>
<TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding deptName}" Margin="2,0,0,0"></TextBlock>
<Expander>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="5*"/>
</Grid.RowDefinitions>
<DataGrid Name="dgdItem" ItemsSource="{Binding Path=choseItems, RelativeSource={RelativeSource FindAncestor,AncestorType=DataGrid,AncestorLevel=1}}" AutoGenerateColumns="False"
CanUserSortColumns="False" >
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="#0C8580"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns >
<DataGridTextColumn Header="选项名" Binding="{Binding ChoseName}" IsReadOnly="True" />
<DataGridTextColumn Header="选项内容" Binding="{Binding ChoseContent}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="1" Content="添加" Height="20" Width="60"/>
</Grid>
</Expander>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
树节点里datagrid始终不显示数据,但是树节点的绑定却是没有问题的,界面效果如下
数据源后台添加的数据如下
List<DepartmentModel> dplst = new List<DepartmentModel>(){
new DepartmentModel(){id=1,deptName="工位类型",ParentId=0,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=2,deptName="通道2",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=3,deptName="通道1_1",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=4,deptName="通道1_2",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=5,deptName="通道1_3",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=6,deptName="通道1_4",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=7,deptName="通道1_5",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=8,deptName="通道2_1",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=9,deptName="通道2_2",ParentId=1,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=10,deptName="设备2_3",ParentId=2,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=11,deptName="设备2_4",ParentId=3,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=12,deptName="设备1_1_1",ParentId=4,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=13,deptName="设备1_1_2",ParentId=5,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=14,deptName="设备1_2_1",ParentId=6,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}},
new DepartmentModel(){id=15,deptName="设备1_1_1_1",ParentId=7,choseItems=new DGDModel{ChoseName = "ChoseName",ChoseContent = "ChoseContent"}}
};