2011-01-18 14 views
1
<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"コントロールにプログラムでアクセスするにはどうすればよいですか?wpfのコントロール内のオブジェクトにプログラムでアクセスするにはどうすればよいですか?

答えて

1

私はLinqToVisualTree使用することをお勧めします...

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

あなたは次のようにタブコントロール内のすべての "border_Main" のコントロールを見つけることができます:あなたが使用することができれば、

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

しますか、単一のborder_Mainインスタンスを検索する場合は、TabItemの上で直接クエリを実行します。

関連する問題