开发者

How to add a wpf control to templated tab control at runtime?

开发者 https://www.devze.com 2023-02-04 06:50 出处:网络
My Tab control has a resource like this: </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property=\"IsSelected\" Value=\"True\">

My Tab control has a resource like this:

                        </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="Te开发者_开发百科xtBlock" 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>

How can I add a control like "Label" to Grid(in Resource with name of "Grid1") of my TabControl?


Try to search the grid in the VisualTree and add then your control to it. You can use the following helper function to find the grids and check then for the name. Otherwise you can also extend the code for a easier search (by adding a name parameter).

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); 
    } 
} 

Something like:

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


You can add a Loaded event handler:

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

and populate and/or record the reference to the grid in the handler:

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

Special case:

  • event handlers in a resource dictionary
0

精彩评论

暂无评论...
验证码 换一张
取 消