Question

As the question states, I am looking to add Click_Events to buttons in a Generic.Xaml.

However, this is not the typical question that everyone seems to be asking as it is not as simple as that. My buttons are generated at runtime, all 35 of them on a Calender-Control. Each day (Square) on the calendar is going to be a button that, when clicked, will open a popup form displaying the full date and further details.

Here is an example of the calendar control:

http://i.stack.imgur.com/il1V6.jpg

Here is the XAML code:

 <Button IsEnabled="{Binding IsEnabled}" Click="" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="{x:Null}" Foreground="White" Height="72" Width="131">
              <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                  <ContentPresenter
                   VerticalAlignment="Center"
                   HorizontalAlignment="Left">
                    <ContentPresenter.Content>
                     <Grid x:Name="ContentGrid" HorizontalAlignment="Left" >
                        <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="15*" />
                            <ColumnDefinition Width="*" />
                              <ColumnDefinition Width="15*" />
                                </Grid.ColumnDefinitions>
                                  <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" FlowDirection="RightToLeft">
                                    <TextBlock Text="{Binding Date, Converter={StaticResource DateConverter}, ConverterParameter=DAY}"  Grid.Column="0" VerticalAlignment="Top" FontSize="14" Margin="5,5,5,5" FontWeight="Light" >
                                       <TextBlock.Style>
                                         <Style TargetType="{x:Type TextBlock}">
                                           <Style.Triggers>
                                             <DataTrigger Binding="{Binding IsTargetMonth}" Value="false">
                                               <Setter Property="TextBlock.Foreground" Value="Black"></Setter>
                                              </DataTrigger>
                                            </Style.Triggers>
                                          </Style>
                                        </TextBlock.Style>
                                       </TextBlock>
                                     </StackPanel>
                                       <TextBlock Text="{Binding Notes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" VerticalAlignment="Center" FontSize="14"/>
                                   </Grid>
                                 </ContentPresenter.Content>
                               </ContentPresenter>
                             </ControlTemplate>
                           </Button.Template>
                        </Button>

As you can see from the XAML, there is a Click_Event available, but I can't reference another form and can't name the buttons as there will be several, so there is no direct way to reference a method in a class. Furthermore, I have attempted to Bind, but keeps giving me a null binding error.

At the moment, in the Calendar class, I have these methods:

public void BuildCalendar(DateTime targetDate)
    {
        Days.Clear();

        //Calculate when the first day of the month is and work out an 
        //offset so we can fill in any boxes before that.
        DateTime d = new DateTime(targetDate.Year, targetDate.Month, 1);
        int offset = DayOfWeekNumber(d.DayOfWeek);
       if (offset != 1) d = d.AddDays(-offset);

        //Show 5 weeks each with 7 days = 35
       for (int box = 1; box <= 35; box++)
       {
           Day day = new Day { Date = d, Enabled = true, IsTargetMonth = targetDate.Month == d.Month };
           day.PropertyChanged += Day_Changed;
           day.IsToday = d == DateTime.Today;
           Days.Add(day);
           d = d.AddDays(1);
       }
    }

And a Day_Clicked method which I can't seem to reference (If that is what I need to do):

 private void Day_Clicked(object sender, EventArgs e)
    {
        if (Clicked == null) return;

        Clicked(this, new DayClickedEventArgs(sender as Day));
    }

Custom EventHandler:

 public event EventHandler<DayClickedEventArgs> Clicked;

Event Class:

public class DayClickedEventArgs : EventArgs
{
    public Day Day { get; private set; }

    public DayClickedEventArgs(Day day)
    {
        this.Day = day;
    }
}

So to summarise, I need to know how to generate Click_Events for each generated button in the calendar. Click_Events for generated buttons using a Generic.Xaml page.

Was it helpful?

Solution

You have to wait until the UI elements from the ControlTemplate have been created. You can handle the FrameworkElement.ApplyTemplate Method for this as by this stage, the template... has been applied. You then need to use the FindName method to find the controls from the XAML.

You can take a look at the How to: Find ControlTemplate-Generated Elements page on MSDN to help you access the UI elements from the ControlTemplate. Once you have access to your UI elements, I'm guessing that you'll be able to attach the handlers on your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top