简体   繁体   中英

How to reference a click_event for multiple buttons on a generic.xaml?

I have a generic.Xaml theme to create a custom calendar control, which is in another project, referenced to my main WPF form. On this generic page, every day/square on the calendar has been set-up as a button. I want to add a click_event to each button, which will then trigger a method on the form that I have put the calendar control onto.

Here is the calendar:

每个正方形都是一个按钮。

And then 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>
                                                            <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 code, I have left the click_event empty, as that is what I need assistance on, as I am unsure what is needed to reference the method in the other project. I have tried simply putting the method name in and this is the error I received:

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

The method I want to link to (which will open another form):

  private void Day_Click(object sender, RoutedEventArgs e)
    {
        CalendarExtension CE = new CalendarExtension(DateTime.Now, _loggedInUser);
        CE.Show();
    }

My question, as the title states, is what is the best method to reference a Click_Event from a Generic.xaml page?

Ah, the old How do I handle an event for a control declared in generic.xaml ' question again. Well, it's a bit of a pain frankly. You need to implement the Control.ApplyTemplate Method in your CustomControl which is called when the ControlTemplate is applied.

In this method, you'll then need to access the relevant UI elements (it helps if you name them) and programmatically add your event handlers. 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 . I'm guessing that you'll be able to attach the handlers on your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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