简体   繁体   English

将 XAML 属性绑定到代码隐藏变量

[英]Bind XAML property to codebehind variable

I would like to set properties in XAML from my C# code.我想从我的 C# 代码在 XAML 中设置属性。 The way I do it, it totally doesn't work.我这样做的方式,它完全行不通。

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>

Code Behind背后的代码

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

The first you should define the property.首先你应该定义属性。 The second is you should put the bind express in { and please see the code.第二个是你应该把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}" />

You can find all the code in github你可以在github中找到所有代码

More about bind, please see Data binding overview - UWP applications更多关于绑定,请看数据绑定概述 - UWP 应用

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

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