This is my first post on stackoverlow which I consider the best online help site for C# and WPF.
I have researched about six+ hours over the past three days trying to figure out how to attach a button handler event to a button I have defined in a style. My situation seems to be different than all the other examples I have looked at on the web. So here goes and I certainly appreciate your input.
A snippet from MainWindow.xaml where I define within a Grid the following
<ScrollViewer Grid.Column="2" Grid.ColumnSpan="2"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Hidden"
Background="AliceBlue">
<ItemsControl ItemsSource="{Binding LaserDataItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Background="AliceBlue" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
This binding on LaserDataItems will contain a list of the UserControls to display.These are created dynamically as needed. But I actually attempt to do all my work in the code behind of the control (below)
Before I show you the UserControl, here is the Style that I am going to use with the UserControl. My first goal is to create click event on the button after it is loaded
<Style x:Key="HeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Button x:Name="MyButton" Height="20"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here is the userControl
<UserControl x:Class="EClient.Controls.GetAllLaserData"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DataGrid Name="laserDataGrid"
ColumnHeaderStyle="{StaticResource HeaderStyle}"
AutoGenerateColumns="False"
ItemsSource="{Binding }"
IsReadOnly="True"
RowHeaderWidth="0"
Height="567"
VerticalAlignment="Top"
RowHeight="17">
<DataGrid.Columns>
<DataGridTextColumn
Width="53"
Header="{Binding HeaderName}"
Binding="{Binding Name}"></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</UserControl>
Within the DataGrid of this UserControl is a ColumnHeaderStyle which is assigned the template: HeaderStyle. This style has the button which I need to attach a click handler to whenever it is instantiated. How do I do this. I have tried to access it by overriding the OnApplyTemplate method but everything I try returns null. Is it possible that the button has not yet been instantiated when this method runs? There must be a way to get to this button.
When I run this and successfully create a list of these UserControls (which are datagrids) a button can be seen in the first cell (which is the header of each grid).
I also tried using a generic routine that traverses the visual tree but to no avail. Actually is this something that I should pursue, I'm really at a loss for ideas to try.
The following piece was added on 10/26/2013
Although i have found another solution to this problem, I am still interested in resolving it. I really thought it was a pretty good question.
Anyway, I thought I would place a picture of what I was trying to do. Perhaps that would place a better perspective on the problem in hand.