我有这个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中没有添加项目!
答案 0 :(得分:3)
当然有:
<Controls:TreeViewItem>
<Controls:TreeViewItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Loading..." Style="{StaticResource LoadingStyle}" />
</DataTemplate>
</Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>