【发布时间】:2017-03-13 20:44:47
【问题描述】:
我正在使用如下 HubSection:
<HubSection Header="Section1">
<DataTemplate>
<TextBox />
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox />
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox />
</DataTemplate>
</HubSection>
现在我有一个 JSON,我想从中绑定所有 HubSection 中的文本框
下面是 JSON 数据的类:
class RootObject
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
现在根据this 问题和this 文章,我可以很好地使用Loaded 事件为TextBox 如下设置值。
<TextBox Loaded="TextBox_Loaded" />
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
var txtBox = (TextBox)sender;
txtBox.Text = "Some Text";
}
但问题是,如果我在每个 HubSection 中绑定/访问的控件不多,这样做就不好了。
那么有人可以告诉我是否有另一种简单的方法来绑定控件。
【问题讨论】:
-
您可以在数据模板中使用Stack Panel 并将控件添加到堆栈面板。这样,您可以在每个 HubSection 内部拥有更多的控件来绑定或访问。
-
@MattewWu-MSFT 但我仍然有三个
HubSections,我想从单个对象绑定所有HubSection中的所有控件,例如RootObject。 -
@DeepakSharma 您想动态生成
HubSections还是定义为3? -
@rbr94 这是一个定义的计数。我想要的只是绑定上面定义的
RootObject类中的所有HubSection中的所有控件。 -
@DeepakSharma 如果我理解正确的话。这意味着您要将第一个
TextBox.Text属性绑定到RootObject的Text1,第二个绑定到Text2等等?你想用RootObject来填充文本框还是用其他方式?
标签: c# json xaml windows-store-apps