【问题标题】:Binding Properties to xaml TextBox (C++/CX) UWP将属性绑定到 xaml 文本框 (C++/CX) UWP
【发布时间】:2019-09-23 03:19:52
【问题描述】:

我的主页中有以下属性。我正在尝试将其绑定到 xaml 文件中的 TextBox。

property String^ TestProperty
{
 String^ get() { return m_testItem; };
 void set(String^ val) { m_testItem= val; }
}

xaml 代码:

<TextBox Grid.Row="1" Grid.Column="0" x:Name="testVal" x:Uid="testVal" Header="Laser" Text="{x:Bind TestProperty, Mode=TwoWay}"/>

我在控件中看不到任何值。我在这里错过了什么?

【问题讨论】:

  • 查看输出窗口,您可能会发现TestProperty 未解析的错误消息。您是否忘记设置 DataContext 对象?像这样的东西。DataContext = this;在 MainPage 的构造函数中。
  • 这个答案有帮助吗? stackoverflow.com/a/55438146/7325217

标签: uwp windows-10-universal uwp-xaml


【解决方案1】:

我在控件中看不到任何值。我在这里错过了什么?

你只是声明TestProperty,但你没有重视它。您可以在 MainPage 构造方法中获得 TestProperty 值。

MainPage::MainPage()
{
    InitializeComponent();  
    TestProperty = "HelloW";
}

我发现你使用了Mode=TwoWay,所以你需要实现INotifyPropertyChanged接口。更多详细步骤请参考data-binding-in-depth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2016-04-17
    • 2018-02-07
    • 2018-02-06
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多