2016-07-25 84 views
-1

我能夠使用link刷新DataGrid的數據

實現樹視圖現在,我已附上以下數據網格,以它爲展示城市的細節,如面積,人口,時區etc.I能使用示例在從樹視圖中選擇城市名稱時接收事件IsSelected。但是,如何將城市模型(Area,Population,TimeZone)的數據綁定到.xaml中的數據網格?我嘗試直接使用CityViewModel,但它永遠不填充數據.CityViewModel有一個名爲CityTowns的CityTown(帶有道具如Area,Population,TimeZone等等)的屬性,當IsSelected被填充時我將填充屬性CityTowns。僅限Tree View有地區 - >州 - >城市等級。城鎮應該在網格中顯示而不是在樹上。

//DemoWindow.xaml content: 
    <TabControl> 
      <TabItem Header="Load Towns"> 
      <StackPanel Orientation="Horizontal"> 
      <advanced: LoadOnDemandControl/> 
      <DataGrid ItemsSource="{Binding Path=local.CityViewModel.CityTowns}" 
        AutoGenerateColumns="False" IsReadOnly="True" 
       > 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding Path=Popluation}" Header="Popluation"/> 
         <DataGridTextColumn Binding="{Binding Path=Revenue}" Header="Revenue"/> 
         <DataGridTextColumn Binding="{Binding Path=TimeZone}" Header="TimeZone"/> 
         <DataGridTextColumn Binding="{Binding Path=Area}" Header="Area"/> 
        </DataGrid.Columns> 
      </DataGrid> 
      </StackPanel> 
     </TabItem> 
</TabControl> 

//LoadOnDemandCcontrol.xaml: 
      <TreeView ItemsSource="{Binding Regions}"> 
      <TreeView.ItemContainerStyle> 
      <!-- 
      This Style binds a TreeViewItem to a TreeViewItemViewModel. 
      --> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
       <Setter Property="FontWeight" Value="Normal" /> 
       <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="FontWeight" Value="Bold" /> 
       </Trigger> 
       </Style.Triggers> 
      </Style> 
      </TreeView.ItemContainerStyle> 

      <TreeView.Resources> 
      <HierarchicalDataTemplate 
       DataType="{x:Type local:RegionViewModel}" 
       ItemsSource="{Binding Children}" 
       > 
       <StackPanel Orientation="Horizontal"> 
       <Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" /> 
       <TextBlock Text="{Binding RegionName}" /> 
       </StackPanel> 
      </HierarchicalDataTemplate> 

      <HierarchicalDataTemplate 
       DataType="{x:Type local:StateViewModel}" 
       ItemsSource="{Binding Children}" 
       > 
       <StackPanel Orientation="Horizontal"> 
       <Image Width="16" Height="16" Margin="3,0" Source="Images\State.png" /> 
       <TextBlock Text="{Binding StateName}" /> 
       </StackPanel> 
      </HierarchicalDataTemplate> 

      <DataTemplate DataType="{x:Type local:CityViewModel}"> 
       <StackPanel Orientation="Horizontal"> 
       <Image Width="16" Height="16" Margin="3,0" Source="Images\City.png" /> 
       <TextBlock Text="{Binding CityName}" /> 
       </StackPanel> 
      </DataTemplate> 
      </TreeView.Resources> 
     </TreeView> 

//CityTown.cs content: 
public class CityTown 
{ 
    public int Area { get; set; } 
    public int Population { get; set; } 
    public string TimeZone { get; set; } 
    public int Revenue { get; set; } 
    public virtual City City { get; set; } 
} 

//CityViewModel.cs cocntent 
public class CityViewModel : TreeViewItemViewModel 
{ 
    readonly City _city; 

    public CityViewModel(City city, StateViewModel parentState) 
     : base(parentState, false) 
    { 
     _city = city; 
    } 

    public string CityName 
    { 
     get { return _city.CityName; } 
    } 

    private ObservableCollection<CityTown> _CityTowns; 

    public ObservableCollection<CityTown> CityTowns 
    { 
     get { return Database.GetTowns(CityName); } 
     set { _CityTowns = value; } 
    } 

} 

//LoadOnDemandDemoControl.xaml.cs content: 
public partial class LoadOnDemandDemoControl : UserControl 
    { 
    public LoadOnDemandDemoControl() 
    { 
     InitializeComponent(); 

     Region[] regions = Database.GetRegions(); 
     CountryViewModel viewModel = new CountryViewModel(regions); 
     base.DataContext = viewModel; 
    } 
} 
+0

請提供完整的XAML,包括您的'DataContext'聲明。 – Jace

回答

0

我解決它通過定義TreeView XAML元素類的SelectedValuePath屬性和使用的相同的在DataGrid元件 - >ItemsSoruce屬性 - >Path - >SelectedItem.ViewModelDataCollection