如何在运行时向模板化选项卡控件添加wpf控件?

时间:2011-01-12 07:43:48

标签: wpf

我的标签控件有这样的资源:                                                                                                                                                                                                                                                                                                    

                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="White" />
                            <Setter TargetName="Image" Property="Visibility" Value="Visible" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
                            <Setter TargetName="TextBlock" Property="Foreground" Value="Black" />
                            <Setter TargetName="Border" Property="Margin" Value="-2,0,2,-1" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True" SourceName="Border" >
                            <Setter TargetName="Border" Property="Background" Value="White" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="DarkGray" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                   <Grid Background="WhiteSmoke" Name="Grid1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="5"/>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Canvas Background="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}" />

                </Grid> 
                </DataTemplate>

            </Setter.Value>
        </Setter>
    </Style>

如何在TabControl的Grid(在名为“Grid1”的资源中)添加像“Label”这样的控件?

2 个答案:

答案 0 :(得分:1)

尝试在VisualTree中搜索网格,然后将控件添加到其中。您可以使用以下帮助程序函数来查找网格,然后检查名称。否则,您还可以扩展代码以便更轻松地搜索(通过添加名称参数)。

void FindChildFrameworkElementsOfType<T>(DependencyObject parent,IList<T> list) where T: FrameworkElement{             
    DependencyObject child; 
    for(int i=0;i< VisualTreeHelper.GetChildrenCount(parent);i++){             
        child = VisualTreeHelper.GetChild(parent, i); 
        if (child is T) { 
            list.Add((T)child); 
        } 
        FindChildFrameworkElementsOfType<T>(child,list); 
    } 
} 

类似的东西:

List<Grid> list=new List<Grid>();
FindCHildFrameworkElementsOfType<Grid>(this,list)
foreach(Grid grid in list){
    if(grid.Name=="Grid1"){
       // Add here your control
       break;
    }
}

答案 1 :(得分:0)

您可以添加Loaded事件处理程序:

<Grid Background="WhiteSmoke" Name="Grid1" Loaded="Grid1_Loaded">

并填充和/或记录处理程序中对网格的引用:

    private Grid grid1;
    private void grid1_Loaded(object sender, RoutedEventArgs e)
    {
        grid1 = sender as Grid;
        // add label, etc.
    }

特例: