刷新TreeViewItem选择更改的DataGrid数据



我能够使用链接实现树状视图

现在,我已经将下面的数据网格附加到它上,用于显示城市的详细信息,如区域、人口、时区等。我能够在使用示例从树视图中选择城市名称时接收事件IsSelected。但是,我如何将城市模型(Area,Population,TimeZone)的数据绑定到.xaml中的数据网格?我尝试直接使用CityViewModel,但它从未填充数据。CityViewModel有一个名为"CityTowns"的"CityTown"属性的observableCollection(包括Area、Population、TimeZone等道具),当IsSelected被解雇时,我正在填充它。"我的树状视图"只有"地区"->"州"->"城市"层次结构。城镇应该显示在网格中,而不是树中。

    //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="ImagesRegion.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="ImagesState.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="ImagesCity.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;
    }
}

我通过为TreeView-xaml元素类定义SelectedValuePath属性来解决这个问题,并在DataGrid元素->ItemsSoruce属性->Path->SelectedItem.ViewModelDataCollection中使用了相同的属性。

相关内容

  • 没有找到相关文章

最新更新