1

我尝试ComboBox用这段代码绑定一个列表:

var template = new DataTemplate();
var comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
template.VisualTree = comboBoxFactory;
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, this.GetType(), 1);
b.Path = new PropertyPath("DataContext");
comboBoxFactory.SetBinding(ComboBox.ItemsSourceProperty, b);
comboBoxFactory.SetBinding(ComboBox.SelectedItemProperty, new Binding("ClassRoom"));

当我简单地启动窗口时它正在工作,如下所示:

winListOrgane ee = new winListOrgane();
ee.Show();

但是当我尝试将此窗口放在网格容器中时,它不起作用:

_currentWindow = (ModelPage)Activator.CreateInstance(pPage.GetType());
_currentWindow.SetParam(genParam);
_currentWindow.InitPage();

object content = _currentWindow.Content;
_currentWindow.Content = null;
_containerPage.Children.Add((UIElement)content);

我能做些什么来解决这个问题?

谢谢

最好的问候,弗雷德里克

4

1 回答 1

0

看起来它是你RelativeSourceBinding。由于您绑定到特定Window类 - 在这种情况下,它看起来GetType()会返回typeof(winListOrgane)。如果将其更改为typeof(Window),它应该可以工作:

b.RelativeSource = 
    new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1);
于 2013-08-13T21:19:08.530 回答