0
我有此XAML控制:另一個「Items集合必須在使用ItemsSource前空」
<Controls:TreeView x:Name="MachineGroupsTree" Style="{StaticResource MachineGroupStyle}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding SetCurrentManagedObjectNodeCommand}" CommandParameter="{Binding SelectedItem, ElementName=MachineGroupsTree, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Controls:TreeViewItem>
<Controls:TreeViewItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Loading..." Style="{StaticResource LoadingStyle}" />
</DataTemplate>
</Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>
</Controls:TreeView>
有了這個代碼隱藏:
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(
MachineGroupTreeViewControl), new PropertyMetadata(null, new PropertyChangedCallback(
OnItemSourceChanged)));
public IEnumerable ItemsSource {
get {
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set {
ClearValue(ItemsSourceProperty);
SetValue(ItemsSourceProperty, value);
}
}
static void OnItemSourceChanged(object sender,
DependencyPropertyChangedEventArgs args) {
// Get reference to self
MachineGroupTreeViewControl source = (MachineGroupTreeViewControl)sender;
// Add Handling Code
// ---------------------------------------------
// EXCEPTION HERE !!!
source.MachineGroupsTree.ItemsSource = (IEnumerable)args.NewValue;
}
我也得到了著名的「項目集合在使用ItemsSource之前必須爲空「異常,但我不明白爲什麼,xaml中沒有添加項目!
它不是一個項目,它是一個項目的模板 - 我想。那麼這種情況下的解決方案是什麼? – Aaaaaaaa 2011-05-30 11:48:24
哦,你有權利,我只看了模板!謝謝! – Aaaaaaaa 2011-05-30 11:54:46