Another "Items collection must be empty before using ItemsSource"
I have a control with this xaml:
<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>
With this code-behind:
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register( "开发者_JAVA技巧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;
}
And I get the well known "Items collection must be empty before using ItemsSource" exception, but I don't understand why, there is no added item in the xaml!
Sure there is:
<Controls:TreeViewItem>
<Controls:TreeViewItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Loading..." Style="{StaticResource LoadingStyle}" />
</DataTemplate>
</Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>
精彩评论