2017-04-01 28 views
1

如何更改txtPIN.Text UserControl初始化後的C#代碼後面的值。UWP [Xaml]如何訪問Button內部的元素

這裏是XAML

<Button x:Name="btn_pin" Content="Change PIN" Click="button_Click" Foreground="White"> 
      <Button.Template> 
       <ControlTemplate TargetType="Button"> 
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
         <Image Source="/Assets/images/settings/lock.png" Stretch="UniformToFill" Width="16" Height="16"/> 
         <TextBlock x:Name="txtPin" Text="Change PIN" Foreground="White" /> 
        </StackPanel> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 

和C#

public MyUserControl() 
    { 
     this.InitializeComponent(); 
     this.btn_pin.?????????? 
    } 

回答

0

你不這樣做是正確的做法,只是解決您的XAML(我改變了文本的TextBlock的結合):

<Button x:Name="btn_pin" Content="Change PIN" Click="button_Click" Foreground="White"> 
      <Button.Template> 
       <ControlTemplate TargetType="Button"> 
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
         <Image Source="/Assets/images/settings/lock.png" Stretch="UniformToFill" Width="16" Height="16"/> 
         <TextBlock x:Name="txtPin" Text="{TemplateBinding Content}" Foreground="White" /> 
        </StackPanel> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 

並且每當您想要更改文字時,都要像這樣更改按鈕的內容:

public MainPage() 
    { 
     this.InitializeComponent(); 
     btn_pin.Content = "New label"; 
    } 
+0

太棒了!它完全按照我們的需要工作。謝謝 ! –

相關問題