2010-05-28 98 views
1

我有web服務ASMX,並有類:無法施展樹型視圖爲樹型視圖在WPF

Country 

public string Name {get;set;} 
public string Code {get;set;} 
public List<Area> Areas {get;set;} 

Area 

public string Name {get;set;} 
public string Code {get;set;} 
public List<Regions> Provinces {get;set;} 

Provinces 

public string Name {get;set;} 
public string Code {get;set;} 

我其綁定到MZ TreeView控件WPF:

myTree的
Country[] items = new MyService().GetListOfCountries(); 
structureTree.ItemsSource = items; 

代碼:

<UserControl x:Class="ObjectsAndZonesSimpleTree" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

     <Grid> 
     <StackPanel Name="stackPanel1"> 

      <GroupBox Header="Choose" Height="354" Name="groupBox1" Width="Auto"> 

       <TreeView Name="structureTree" SelectedItemChanged="structureTree_SelectedItemChanged" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}" Height="334" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" Width="Auto" PreviewMouseRightButtonUp="structureTree_PreviewMouseRightButtonUp" FontFamily="Verdana" FontSize="12" BorderThickness="1" MinHeight="0" Padding="1" Cursor="Hand" Margin="-1"> 
        <TreeView.Resources> 
         <HierarchicalDataTemplate DataType="{x:Type MyService:Country}" 
            ItemsSource="{Binding Path=ListOfRegions}"> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/> 
          </StackPanel> 
         </HierarchicalDataTemplate> 
         <HierarchicalDataTemplate DataType="{x:Type MyService:Region}" 
            ItemsSource="{Binding Path=Provinces}"> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/> 


       </StackPanel> 
         </HierarchicalDataTemplate> 
         <DataTemplate DataType="{x:Type MyService:Province}" 
            ItemsSource="{Binding Path=ListOfCities}"> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/> 
       </StackPanel> 
         </DataTemplate> 

        </TreeView.Resources> 
       </TreeView> 
      </GroupBox> 

     </StackPanel> 
    </Grid> 

</UserControl> 

這讓我空:

private void structureTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) 
     { 
TreeViewItem treeViewItem = structureTree.SelectedItem as TreeViewItem; 
} 

回答

3

SelectedItem實際上將包含國家,地區或地區(或null)。如果你真的想要TreeViewItem,你可以做strutureTree.ItemContainerGenerator.ContainerFromItem(structureTree.SelectedItem)

+0

我得到錯誤:對象引用是必需的非靜態字段,方法或屬性'System.Windows.Controls.ItemContainerGenerator.ContainerFromItem(object)' – user278618 2010-05-28 13:23:44

+0

您可以發佈您用於此的確切代碼?這聽起來像你試圖做'TreeView.ItemContainerGenerator',但你必須用'structureTree.ItemContainerGenerator'從特定的樹訪問它。 – JustABill 2010-05-28 17:05:48

2

正確。你應該期待一個國家作爲你的SelectedItem。 WPF的工作方式與Windows窗體完全不同。這完全是關於數據綁定!

+0

所以,我不能得到父節點,或做多選? :/ – user278618 2010-05-28 13:27:10

+0

你可以。但我不記得那部分。這絕對是一個痛苦。 – 2010-05-28 14:42:48