WPF: How do I get a reference to a styled window control in code behind?

Posted by Brad on Stack Overflow See other posts from Stack Overflow or by Brad
Published on 2010-02-24T21:48:51Z Indexed on 2010/04/01 1:13 UTC
Read the original article Hit count: 429

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about style