开发者

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜