如何以编程方式访问wpf控件中的对象?

时间:2011-01-18 06:12:42

标签: wpf tabcontrol

<TabControl x:Class="MyTabControl.Tab_Control"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyTabControl" Padding="0" Background="White" BorderBrush="Black" Loaded="TabControl_Loaded">
<TabControl.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid Name="Grid_Main">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="1" Name="border_Main" Background="#F0F0F0" BorderBrush="LightGray" BorderThickness="1,1,1,0" CornerRadius="4,4,0,0"
                                Margin="-2,0,2,0" SnapsToDevicePixels="True" >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontSize="13" HorizontalAlignment="Center" Name="TextBlock"
                                           Foreground="DarkGray"> 
                                    <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" 
                                                      HorizontalAlignment="Center" ContentSource="Header" Margin="5,2,5,2"/></TextBlock>
                                </StackPanel>

                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="border_Main" Property="Background" Value="White" />
                                <Setter TargetName="border_Main" Property="BorderBrush" Value="Gray" />
                                <Setter TargetName="TextBlock" Property="Foreground" Value="Black" />
                                <Setter TargetName="border_Main" Property="Margin" Value="-2,0,2,-1" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True" SourceName="border_Main" >
                                <Setter TargetName="border_Main" Property="Background" Value="White" />
                                <Setter TargetName="border_Main" Property="BorderBrush" Value="DarkGray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>
    </TabControl.Resources>
</TabControl>

如何以编程方式访问“border_Main”控件?

1 个答案:

答案 0 :(得分:1)

我建议使用LinqToVisualTree ...

http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/

您可以在TabControl中找到所有“border_Main”控件,如下所示:

tabControl.Descendants<Border>().Where(d => d.Name=="border_Main");

或者,如果要在TabItem上直接使用上述查询,如果要查找单个border_Main实例。