2017-08-10 31 views
1

我是UWP的新手,並嘗試使用它。如果它太基本,請點我鏈接。 我正在開發一個自定義控件(UWP模板控件),帶有一些文本框和按鈕。理想情況下,我想在我的MainPage中使用此控件作爲標題控件,具體取決於Templatecontrol中每個按鈕的單擊,我想呈現不同的頁面。 現在來基本的問題,我如何聯播事件處理程序中CustomControl 這是我的Generic.xaml:(Project1.Library)帶事件處理程序的UWP模板控制

<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中加載這些康特編寫代碼nt或Frame。

+0

給定答案有什麼問題? –

回答

2

如何將一個按鈕單擊事件添加到模板控制

名稱添加到該按鈕,讓你可以在對象後面的代碼

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