Chen住气* 2017-12-20 02:12 采纳率: 100%
浏览 1158
已采纳

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 2017-12-23 14:37
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错