0

I have a rather interesting case with ComboBox control - CustomComboBox;

In the style of this ComboBox, Popup contains one custom control that requests a DataContext;

<ctrl:CustomGrid DataContext="{TemplateBinding DataContext}" GridName="{Binding Preferences.CurrentGridName}"/>

The idea:

  • to use this control several times on one page
  • to use it in a masterpage container
  • the masterpage control needs to have different DataContexts regarding the Page it is on

The logic:

  • In the overriden OnApplyTemplate I am getting the grid and connecting few eventhandlers

The problem:

  • The masterpage control is triggering OnApplyTemplate only once
  • The first appearance of the CustomComboBox is as expected. However, every next apearance is with same DataContext, even when changing the datacontext of the CustomComboBox These changes don't reach to change my CustomGrid DataContext

I am sure that something on the bindings or the presentation logic is bad... Please throw some thoughts on, I would appreciate a hint here

Thanks

4

1 回答 1

1

OnApplyTemplate当 aControlTemplate应用于覆盖该方法的控件(既不是它的父项,也不是子项)时调用。如果OnApplyTemplate输入一次,则覆盖控件也必须创建一次。我的意思是你只有一个母版页实例。这不应该是意外的。

谈到Popups 和,从 a到外部DataContext的绑定经常存在问题。Popup因此,我宁愿编写一些代码隐藏来为Popups 提供正确的上下文,而不是依赖于Bindings。在 SL5 之前肯定存在 DataContextChanged 事件缺失的问题。要解决这个问题,您应该DependencyProperty在 . 上定义自定义CustomComboBox,将其绑定到CustomComboBox的上下文并将其值分配PopupPropertyChangedCallback.

于 2011-12-21T15:50:45.613 回答