Chenrongsake
忧伤的熊二
2017-12-20 02:12
采纳率: 100%
浏览 1.1k
已采纳

wpf树模板里datagrid绑定问题

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"}}
            };
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • hxycsdn9159
    hxycsdn9159 2017-12-23 14:37
    已采纳

    你的绑定表达式 ItemsSource="{Binding Path=choseItems, RelativeSource={RelativeSource FindAncestor,AncestorType=DataGrid,AncestorLevel=1}}" 就有问题,从你粘贴出来的前台xaml里面 你用“RelativeSource FindAncestor,AncestorType=DataGrid” 查找父级里面类型为DataGri
    d的元素根本就查找不到,所以怎么会显示呢

    点赞 评论
  • caozhy
    点赞 评论

相关推荐