<UserControl x:Class="Newauto.BSEditController.LeftTreeView"
<DockPanel Name="dockPanel">
<DockPanel.Resources>
<ObjectDataProvider x:Key="MyList" ObjectInstance="_this.DataContext" />
<HierarchicalDataTemplate DataType = "{x:Type BSEditController:StationInfoMode}"
ItemsSource = "{Binding Path=AgentInfoModes}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType = "{x:Type BSEditController:AgentInfoMode}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
</DockPanel.Resources>
<TreeView Name="treeViewMyList" SelectedItemChanged="TreeView_SelectedItemChanged">
<TreeViewItem Name="treeviewItem" ItemsSource="{Binding Source={StaticResource MyList}}" Header="HeaderShow" />
</TreeView>
<Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click_1" />
</DockPanel>
后台更新
private void button1_Click_1(object sender, RoutedEventArgs e)
{
this.DataContext = new StationInfoModes();
ObjectDataProvider provider = dockPanel.FindResource("MyList") as ObjectDataProvider;
if (provider != null)
{
provider.ObjectInstance = new StationInfoModes();
}
}
<UserControl.Resources>
<ObjectDataProvider x:Key="dataProvider"
ObjectType="loc:FileSystemObjectViewModel"
MethodName="GetSystemDrives"/>
<loc:IconConverter x:Key="iconConverter" />
</UserControl.Resources>
<TreeView VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
DataContext="{Binding Source={StaticResource dataProvider}}"
ItemsSource="{Binding Children}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="loc:FileSystemObjectViewModel"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource iconConverter}}" />
<TextBlock Text="{Binding DisplayName}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>