如何在wpf中的自定义窗口中查找控件

时间:2015-03-17 08:16:58

标签: c# wpf xaml wpf-controls


我为Window设计了一个表单模板,并命名为" WindowStyle1":

<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border Height="Auto" Width="Auto" Background="#FF27C986" Margin="10">
                    <Border.Effect>
                        <DropShadowEffect ShadowDepth="0" Direction="180" Opacity="0.52" BlurRadius="10"/>
                    </Border.Effect>
                    <Grid Loaded="OnWindowLoaded">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="94"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="38"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.RowSpan="1" Grid.Row="1" Grid.ColumnSpan="2">
                            <AdornerDecorator>
                                <ContentPresenter/>
                            </AdornerDecorator>
                        </Border>
                        <Button Click="minimize_window" x:Name="btn_window_minimize" Grid.Column="1" HorizontalAlignment="Left" Margin="4.565,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_MinimumSize}" BorderThickness="1"/>
                        <Button Click="maximize_window" x:Name="btn_window_maximize" Grid.Column="1" HorizontalAlignment="Left" Margin="34.565,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_MaximumSize}"/>
                        <Button Click="close_window" x:Name="btn_window_close" Grid.Column="1" HorizontalAlignment="Left" Margin="64,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_Close}"/>
                        <Grid MouseLeftButtonDown="Window_MouseLeftButtonDown" HorizontalAlignment="Stretch" Height="38" VerticalAlignment="Top" Background="#FF27C986">
                            <Label Name="lbl_title" Content="Label" HorizontalAlignment="Left" Margin="0.998,0,0,0" VerticalAlignment="Top" FontSize="20" Foreground="White"/>
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/>
        </Trigger>
    </Style.Triggers>
</Style>

并将此.xaml文件绑定到&#34; Window.cs&#34;课程:

namespace WpfApplication2
{
    partial class Window : ResourceDictionary
    {
        public Windows()
        {
            InitializeComponent();
        }
    }
}

但是我无法在我的&#34; MainWindow.xaml&#34;中找到按钮控件。

主要问题:
如何在wpf中找到在自定义窗口样式中定义的标签控件

任何想法? 对不起英语不好!

3 个答案:

答案 0 :(得分:2)

您需要在Loaded事件(或更高版本)中执行此操作。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var label = Template.FindName("lbl_title", this) as Label;
        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var label = Template.FindName("lbl_title", this) as Label;
    }
}

答案 1 :(得分:0)

尝试使用:

this.GetTemplateChild("minimize_window");

您可以找到有关此方法的更多信息here.

  

WPF中的模板具有自包含的名称范围。这是因为模板被重用,并且当控件的多个实例每个实例化其模板时,模板中定义的任何名称都不能保持唯一。

答案 2 :(得分:0)

您可以使用FrameworkTemplate.FindName Method找到已在ControlTemplate中定义的元素,其中查找与此模板中定义的指定名称相关联的元素

您可以在MSDN上的How to: Find ControlTemplate-Generated Elements页面中查看完整的故事,但这是一个从链接页面中获取的简短示例,以便您了解:

// Finding the grid that is generated by the ControlTemplate of the Button
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
// Do something to the ControlTemplate-generated grid
MessageBox.Show("The actual width of the grid in the ControlTemplate: "
    + gridInTemplate.GetValue(Grid.ActualWidthProperty).ToString());