简体   繁体   English

UWP [Xaml]如何访问Button内的元素

[英]UWP[Xaml] How to access elements inside Button

How to change txtPIN.Text value in C# code behind after UserControl was initialized. 初始化UserControl后,如何更改C#代码中的txtPIN.Text值。

Here is XAML 这是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>

and C# 和C#

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

You're not doing it the right way, just fix your xaml (I changed the Text binding of your TextBlock): 你没有以正确的方式做到这一点,只需修复你的xaml(我更改了TextBlock的Text绑定):

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

and every time you want to change the text, change the Content of the button like this: 每次要更改文本时,请更改按钮的内容,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM