带有事件处理程序的 UWP 模板控制 [英] UWP Template Control with Event Handler
问题描述
我是 UWP 的新手并正在尝试.如果它太基本,请指向我的链接.我正在开发一个带有一些文本框和按钮的自定义控件(UWP 模板控件).理想情况下,我想在我的 MainPage 中将此控件用作 Header Control,根据 Templatecontrol 中的每个按钮单击,我想呈现不同的页面.现在来到基本问题,如何在 CustomControl 中连接事件处理程序这是我的 Generic.xaml: (Project1.Library)
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:
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)
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>
我想访问的视图在 Project1 中可用,因此我想在 MainPage.xaml.cs 上编写代码以加载这些内容或框架.
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.
推荐答案
如何给模板控件添加按钮点击事件
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);
}
这篇关于带有事件处理程序的 UWP 模板控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!