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

发布于 2024-10-13 03:54:01 字数 3217 浏览 3 评论 0原文

<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”控件?

<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>

How can I access my "border_Main" control programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-10-20 03:54:01

我建议使用 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");

或者,如果您可以如果您想查找单个 border_Main 实例,请直接对 TabItem 使用上述查询。

I would recommend using LinqToVisualTree ...

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

You can find all the "border_Main" controls within the TabControl as follows:

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

Or, if you could use the above query on a TabItem directly if you want to find a single border_Main instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文