0

我想从我的 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"     
    }
  }
}
4

1 回答 1

0

首先你应该定义属性。第二个是你应该把绑定快递放进去{,请看代码。

    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 应用程序

于 2020-03-19T10:45:23.973 回答