代码之家  ›  专栏  ›  技术社区  ›  Nick

在运行时添加复合WPF区域

  •  0
  • Nick  · 技术社区  · 17 年前

            <TabControl  x:Name="tabDemo" ItemsSource="{Binding DynamicRegions}" >
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ItemsControl cal:RegionManager.RegionName="{Binding}" ItemsSource="{x:Null}">
    
                    </ItemsControl>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    

       private void AddDynamicRegion(object arg)
      {
         var newRegionName = "Region" + (DynamicRegions.Count + 1).ToString();
         DynamicRegions.Add(newRegionName);
      }
    
      private void AddRandomRegionContent(object arg)
      {
         if (string.IsNullOrEmpty(SelectedRegion) )
            return; 
    
         Debug.WriteLine("Injected "  + RegionContent + " into " + SelectedRegion);
    
         var newContent = new TextBlock() { Text = RegionContent };
         _regionManager.RegisterViewWithRegion(SelectedRegion,() => newContent );
    
         _regionManager.Regions[SelectedRegion].Activate(newContent);
      }
    

    3 回复  |  直到 17 年前
        1
  •  2
  •   Anderson Imes    17 年前

        2
  •  1
  •   Jeremiah Morrill    17 年前

         <TabControl cal:RegionManager.RegionName="TabRegion" x:Name="tabDemo" ItemsSource="{Binding Something}" >
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </TabControl.ItemTemplate>
         </TabControl>
    

    Prism中的StockChartsRI显示了如何从视图模型中绑定选项卡的标题。它们使用TabPanel的子类(AnimatedTabPanel),但设置是相同的。

        3
  •  0
  •   Nick    17 年前

    基本上,当使用数据绑定生成tabItems时,我在代码中设置了所有区域内容。

      private void AddDynamicRegion(object arg)
      {
         var newRegionName = "Region" + (DynamicRegions.Count + 1).ToString();
         DynamicRegions.Add(newRegionName);
    
         var tabItem = View.tabDemo.ItemContainerGenerator.ContainerFromIndex(DynamicRegions.Count - 1) as TabItem;
         var newRegionContainer = new ItemsControl();
         RegionManager.SetRegionName(newRegionContainer,newRegionName);
         RegionManager.SetRegionManager(newRegionContainer, _regionManager);
         tabItem.Content = newRegionContainer;
    
    
      }
    

      private void AddRandomRegionContent(object arg)
      {
    
         if (string.IsNullOrEmpty(SelectedRegion) )
            return; 
    
         Debug.WriteLine("Injected "  + RegionContent + " into " + SelectedRegion);
    
         var newContent = new TextBlock() { Text = RegionContent };
         var region = _regionManager.Regions[SelectedRegion];
         if (region == null)
         {
            Debug.WriteLine("Couldn't find region");
            return;
         }
    
         region.Add(newContent);
         region.Activate(newContent);
    
      }
    
        4
  •  0
  •   David Veeneman    17 年前