问题描述
主界面是一个TableControl控件,TableItems根据按键动态添加,所添加的TableItems为UserControl,UserControl类似view层,不过可以嵌入window里,UserControl有自己的datacontext,在其下的控件采用{binding path=XX},并没有产生绑定效果
问题解决
经过调试检查,DataContext设置正确,猜测问题是Binding时未明确依赖属性的DataContext,所以导致其不清楚该绑定到哪个,因为绑定外层DataContext的某些属性亦无效果。采用如下Binding形式即可,明确制定未UserControl控件的DataContext,问题解决
{Binding DataContext.filepath,RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Mode=TwoWay}