问题描述
|
我对此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之前,Items集合必须为空”的异常,但是我不明白为什么,xaml中没有添加的项!
解决方法
当然有:
<Controls:TreeViewItem>
<Controls:TreeViewItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text=\"Loading...\" Style=\"{StaticResource LoadingStyle}\" />
</DataTemplate>
</Controls:TreeViewItem.HeaderTemplate>
</Controls:TreeViewItem>