簡體   English   中英

將 XAML 屬性綁定到代碼隱藏變量

[英]Bind XAML property to codebehind variable

我想從我的 C# 代碼在 XAML 中設置屬性。 我這樣做的方式,它完全行不通。

XAML

<Page
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="x:bind width_background" Height="x:bind height_background">
<Grid>
    <TextBox x:Name="Textbox1" Text="x:bind text_textbox1" /> 
    <Border BorderThickness="1" Height="x:bind height_border" Width="x:bind width_border"/>
</Grid>
</Page>

背后的代碼

namespace Test
{
  public sealed partial class MainPage : Page
  {

    public MainPage()
    {
        this.InitializeComponent();

        int height_background = 500;
        int width_background = 600;
        int height_border = 500;
        int width_border = 600;  
        string text_textbox1 = "string test"     
    }
  }
}

首先你應該定義屬性。 第二個是你應該把bind express放在{ ,請看代碼。

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof(string), typeof(MainPage), new PropertyMetadata(default(string)));

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    <TextBox x:Name="Textbox1" Text="{x:Bind Text}" />

你可以在github中找到所有代碼

更多關於綁定,請看數據綁定概述 - UWP 應用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM