這很奇怪。以下代碼的要點是支持附加屬性,該附加屬性將通知容器是否有任何兒童已獲得焦點。LogicalTreeHelper.GetChildren - ObservableCollection Move()導致DataTemplate中的ContentControl失去其內容?
即我有一個網格,一個TextBox某處它的內容,我想轉網格藍如果這些控件之一獲得焦點。
我有一個ListView與ItemsTemplate。 ItemsTemplate是一個包含一些東西的DataTemplate ......但其中一個是ContentControl。
實施例:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Border>
<ContentControl Content="{Binding Something}"/>
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在ContentControl中的結合應顯示特定類型的用戶控件的。 創建時...工作得很好。如果我遞歸迭代從Grid元素開始的listViewItem的模板,它也會遍歷ContentControl的「Content」。
但是,一旦我在ListView ItemsSource綁定的ObservableCollection上執行.Move(),根據LogicalTreeHelper,ContentControl.Content爲空。
什麼給?
如果我檢查ContentControl中,它讓我看到的內容...但LogicalTreeHelper.GetChildren返回空枚舉。
我很困惑...
任何人都可以解釋爲什麼會出現這種情況?
LogicalTreeHelper iterator方法
public static void applyFocusNotificationToChildren(DependencyObject parent)
{
var children = LogicalTreeHelper.GetChildren(parent);
foreach (var child in children)
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement == null)
continue;
Type frameworkType = frameworkElement.GetType();
if (frameworkType == typeof(TextBox) || frameworkType == typeof(ListView) ||
frameworkType == typeof(ListBox) || frameworkType == typeof(ItemsControl) ||
frameworkType == typeof(ComboBox) || frameworkType == typeof(CheckBox))
{
frameworkElement.GotFocus -= frameworkElement_GotFocus;
frameworkElement.GotFocus += frameworkElement_GotFocus;
frameworkElement.LostFocus -= frameworkElement_LostFocus;
frameworkElement.LostFocus += frameworkElement_LostFocus;
// If the child's name is set for search
}
applyFocusNotificationToChildren(child as DependencyObject);
}
}