2016-07-25 54 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