另一个“使用 ItemsSource 之前 Items 集合必须为空”

发布于 2024-11-10 14:29:30 字数 2112 浏览 2 评论 0原文

我有一个带有此 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>

使用此代码隐藏:

            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 中没有添加项目!

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( "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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-11-17 14:29:30

当然有:

<Controls:TreeViewItem>
    <Controls:TreeViewItem.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Loading..." Style="{StaticResource LoadingStyle}" />
        </DataTemplate>
    </Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>

Sure there is:

<Controls:TreeViewItem>
    <Controls:TreeViewItem.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Loading..." Style="{StaticResource LoadingStyle}" />
        </DataTemplate>
    </Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文