如何将Click_Events添加到按钮中,在Generic.Xaml中添加了运行时?

时间:2014-01-22 10:18:54

标签: c# wpf xaml mouseclick-event generic.xaml

正如问题所述,我希望将Click_Events添加到Generic.Xaml中的按钮。

然而,这不是每个人似乎都会问的典型问题,因为它不是那么简单。我的按钮是在运行时生成的,所有35个按钮都在Calender-Control上生成。日历上的每一天(Square)将成为一个按钮,单击该按钮将打开一个弹出窗体,显示完整日期和更多详细信息。

以下是日历控件的示例:

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

以下是XAML代码:

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

从XAML可以看到,有一个Click_Event可用,但是我不能引用另一个表单而不能命名按钮,因为会有几个,所以没有直接的引用方式一个类中的方法。此外,我试图绑定,但一直给我一个空绑定错误。

目前,在Calendar类中,我有以下方法:

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);
       }
    }

我似乎无法参考的Day_Clicked方法(如果这是我需要做的):

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

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

自定义EventHandler

 public event EventHandler<DayClickedEventArgs> Clicked;

活动类:

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

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

总而言之,我需要知道如何为日历中的每个生成按钮生成Click_EventsClick_Events用于使用Generic.Xaml页面生成的按钮。

1 个答案:

答案 0 :(得分:0)

您必须等到ControlTemplate中的UI元素已创建。您可以为此处理FrameworkElement.ApplyTemplate Method,因为此阶段已经应用了模板....然后,您需要使用FindName方法从XAML中找到控件。

您可以查看MSDN上的How to: Find ControlTemplate-Generated Elements页面,以帮助您从ControlTemplate访问UI元素。一旦您可以访问UI元素,我猜您将能够自己附加处理程序。