简体   繁体   English

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

[英]How to Add Click_Events to buttons added a runtime in Generic.Xaml?

As the question states, I am looking to add Click_Events to buttons in a Generic.Xaml . 由于该问题时,我期待加入Click_Events在一个按钮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. 我的按钮是在运行时生成的,所有35个按钮都在日历控件上。 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 http://i.stack.imgur.com/il1V6.jpg

Here is the XAML code: 这是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>

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. 从XAML中可以看到,有一个Click_Event可用,但是我不能引用另一种形式,也不能命名按钮,因为会有多个按钮,因此没有直接的方法可以在类中引用方法。 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: 目前,在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);
       }
    }

And a Day_Clicked method which I can't seem to reference (If that is what I need to do): 还有我似乎无法引用的Day_Clicked方法(如果那是我需要做的):

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

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

Custom EventHandler : 自定义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 ,我需要知道如何为Click_Events中的每个已生成按钮生成Click_Events Click_Events for generated buttons using a Generic.Xaml page. Click_Events使用生成按钮Generic.Xaml页。

You have to wait until the UI elements from the ControlTemplate have been created. 您必须等待,直到已经创建ControlTemplate的UI元素。 You can handle the FrameworkElement.ApplyTemplate Method for this as by this stage, the template... has been applied. 您可以为此处理FrameworkElement.ApplyTemplate方法 ,因为到此阶段,已经应用了模板...。 You then need to use the FindName method to find the controls from the XAML. 然后,您需要使用FindName方法从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. 您可以查看MSDN上的“ 如何:查找ControlTemplate生成的元素”页面,以帮助您从ControlTemplate访问UI元素。 Once you have access to your UI elements, I'm guessing that you'll be able to attach the handlers on your own. 一旦您可以访问UI元素,我猜您将能够自行附加处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM