WPF,DataGrid里RowDetailsTemplate嵌套DataGrid的绑定问题

就是月表里前了好几个日表,想用datagrid的明细显示另一个datagrid
前台:
<DataGrid ItemsSource="{Binding MonthPlanCollection}" AutoGenerateColumns="True">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding WeekPlanMonth}"
AutoGenerateColumns="True"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>

MonthPlanModel类型:
public class MonthPlanModel
{
public int ID { set; get; }
public string Plan_ID { set; get; }
。。。。等等
public ObservableCollection<WeekPlanModel> WeekPlanMonth { set; get; }
}
WeekPlanModel的元素ID,Name之类
为什么字datagrid里显示不了数据呢?显示出空表的行数就是WeekPlanMonth的元素个数,按理来说数据已经传过去了。为什么是空的呢?里面有数据,至少这行是真有!也就是这里MonthPlanCollection的赋值是没问题的。
求高人解答!

第1个回答  2013-10-12
应该是数据的变化没有更新到客户端的原因,检查一下INotifyPropertyChanged接口、PropertyChanged事件,希望对你有帮助,如还有疑问可百度Hi上联系追问

我用mvvm模式的,其他的绑定都没有问题,应该不是这个原因,哎。。。

追答

MVVM那就是ViewModel写得有问题了,界面上只需要Binding,绑定到ObservableCollection列表

第2个回答  推荐于2016-08-12

你好:

        我试了一下是可以正常显示的。不知道你的变量WeekPlanMonth和类WeekPlanModel是什么样子的。


我的代码

前台:

<DataGrid ItemsSource="{Binding MonthPlanCollection}"  AutoGenerateColumns="True">
     <DataGrid.RowDetailsTemplate>
           <DataTemplate>
                <DataGrid ItemsSource="{Binding WeekPlanMonth}" AutoGenerateColumns="True"/>
           </DataTemplate>
     </DataGrid.RowDetailsTemplate>
</DataGrid>

VM:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var weekPm = new ObservableCollection<WeekPlanModel>();
            weekPm.Add(new WeekPlanModel(){ID = 1,Plan_ID = "One"} );
            weekPm.Add(new WeekPlanModel() { ID = 2, Plan_ID = "Two" });
            weekPm.Add(new WeekPlanModel() { ID = 3, Plan_ID = "Three" });

            MonthPlanCollection = new ObservableCollection<MonthPlanModel>();
            MonthPlanCollection.Add(new MonthPlanModel() {ID = 1,Plan_ID = "A",WeekPlanMonth = weekPm});
            MonthPlanCollection.Add(new MonthPlanModel() { ID = 2, Plan_ID = "B" });
            MonthPlanCollection.Add(new MonthPlanModel() { ID = 3, Plan_ID = "C" });
            DataContext = this;
        }

        public ObservableCollection<MonthPlanModel> MonthPlanCollection { get; set; }
    }

     public class MonthPlanModel
    {
        public int ID { set; get; }
        public string Plan_ID { set; get; }
        public ObservableCollection<WeekPlanModel> WeekPlanMonth { set; get; }
    }
     public class WeekPlanModel
     {
         public int ID { set; get; }
         public string Plan_ID { set; get; }
     }

效果:

追问

太感谢啦

我的属性定义不是在window的后台文件里的,其他的都跟你的类似,就是显示不出数据。
也就是说这种ObservableCollection里带ObservableCollection的绑定是没问题的哈?

追答

在后台写属性只是为了方便,不要再新建文件了。这个没什么影响。

这种ObservableCollection里带ObservableCollection的绑定是没问题的。

本回答被提问者采纳
相似回答