views:

162

answers:

1

I have a window defined with a style:

<Window x:Class="winBorderless"
        x:Name="winBorderless"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Local="clr-namespace:WindowStyle"
        Style="{StaticResource Window_Cartesia}"
        WindowStartupLocation="CenterScreen"
        BorderThickness="1"
        BorderBrush="#FF9CAAC1"
        Margin="5"
        Title="[Document Title]">

and the style defined in an application level dictionary:

 <Style x:Key="Window_Cartesia" TargetType="{x:Type Window}">

  <Setter Property="WindowStyle" Value="None"/>
  <Setter Property="AllowsTransparency" Value="True"/>
  <Setter Property="Background" Value="Transparent"/>

  <EventSetter Event="Loaded" Handler="Loaded"/>  
  <EventSetter Event="PreviewKeyDown" Handler="Preview_KeyDown"/>
  <EventSetter Event="MouseMove" Handler="FullScreen_MouseMove"/>

  <Setter Property="Template">

In code behind I have a reference to the Window instance set:

 Win = DirectCast(sender, winBorderless)

This allows access to the window properties as the EventSetters pass references to the various controls. However, it doesn't provide for access to the controls defined in the style through the window reference as they don't exist there.

So, what is the best way to reference a control through code behind that is defined in the style. I'd prefer not to iterate the trees to find them but ya gotta do....

+1  A: 

I am assuming that want to access the Controls that originally where defined in the ControlTemplate of you Window. In that case your best bet is to use VisualTreeHelper.GetChild() Additionally, using FrameworkElement.TemplatedParent you can check if the child was created from a ControlTemplate.

bitbonk