UWP Template Control with Event Handler

2020-08-01 04:46发布

I am new to UWP and trying out. Please point me to links if its too basic. I am developing an Custom Control ( UWP Template Control ), with some Text box and Buttons. Ideally, I want to use this control in my MainPage as Header Control, depending on each button click in Templatecontrol, I want to render different pages. Now coming to basic question, How do I hookup the event handler in CustomControl This is my Generic.xaml: (Project1.Library)

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP.CustomControls.Library">

<Style TargetType="local:MyCustomControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyCustomControl">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{TemplateBinding FirstName}" Margin="8 8 2 8" />
                        <Button Content="Go!" Click="MyCustomControl_Click" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

MyCustomContro.cs:

  public string FirstName
    {
        get { return (string)GetValue(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value); }
    }

    public static readonly DependencyProperty FirstNameProperty =
        DependencyProperty.Register("FirstName", typeof(string), typeof(MyCustomControl), new PropertyMetadata(string.Empty));

public event EventHandler Click;

MainPage.xaml: ( Project1)

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <controls:MyCustomControl Width="400" Height="35" Background="Orange" 
    Margin="20" FirstName="MyFName" LastName="MyLName" Click="MyCustomControl_Click"/>

    <Button Content="Show!" x:Name="Text1" />
</Grid>

Views, I want to access are available in Project1 and so I want to write code on MainPage.xaml.cs to load those Content or Frame.

标签: uwp
1条回答
Ridiculous、
2楼-- · 2020-08-01 05:18

How to add a button click event to a template control

Add A name to the button so you can get to the object in the code behind

  <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWP.CustomControls.Library">

    <Style TargetType="local:MyCustomControl" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyCustomControl">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{TemplateBinding FirstName}" Margin="8 8 2 8" />
                            <Button Name:"_BtnGo" Content="Go!" Click="MyCustomControl_Click" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </ResourceDictionary>

Added a pointer to button control and have the buttons event passed to the event handler in the control

private  Button _BtnGo;

public string FirstName
    {
        get { return (string)GetValue(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value); }
    }

    public static readonly DependencyProperty FirstNameProperty =
        DependencyProperty.Register("FirstName", typeof(string), typeof(MyCustomControl), new PropertyMetadata(string.Empty));

public event EventHandler Click;

    public event EventHandler<RoutedEventArgs> GoClicked;


     protected override void OnApplyTemplate()
        {
        _BtnGo = GetTemplateChild(nameof(_BtnGo)) as Button;
        _BtnGo.Click += (s, e) => GoClicked?.Invoke(s, e);
    }
查看更多
登录 后发表回答