【问题标题】:UWP Template Control with Event Handler带有事件处理程序的 UWP 模板控件
【发布时间】:2018-01-19 07:27:08
【问题描述】:

我是 UWP 的新手并正在尝试。如果它太基本,请指出我的链接。 我正在开发一个带有一些文本框和按钮的自定义控件(UWP 模板控件)。理想情况下,我想在我的 MainPage 中将此控件用作 Header 控件,根据 Templatecontrol 中的每个按钮单击,我想呈现不同的页面。 现在来到基本问题,如何在 CustomControl 中连接事件处理程序 这是我的 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>

我想访问的视图在 Project1 中可用,因此我想在 MainPage.xaml.cs 上编写代码来加载这些内容或框架。

【问题讨论】:

  • 给出的答案有什么问题?

标签: uwp


【解决方案1】:

如何向模板控件添加按钮点击事件

为按钮添加一个名称,以便您可以在后面的代码中访问对象

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

添加了一个指向按钮控件的指针,并将按钮事件传递给控件中的事件处理程序

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 2018-02-20
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多