另一个“使用 ItemsSource 之前 Items 集合必须为空”
我有一个带有此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然有:
Sure there is: