Привязать свойство XAML к переменной codebehind - PullRequest
0 голосов
/ 19 марта 2020

Я хотел бы установить свойства в XAML из моего C# кода. То, как я это делаю, совершенно не работает.

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

1 Ответ

0 голосов
/ 19 марта 2020

Сначала вы должны определить свойство. Во-вторых, вы должны поместить 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

Подробнее о bind см. Обзор привязки данных - приложения UWP

...