如何在 Windows 8.1 中访问 HubSection 内的控件
How to access controls inside HubSection in Windows 8.1
我正在使用如下的 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:
class RootObject
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
现在根据 this question and this 文章,我可以很好地为 TextBox
使用 Loaded
事件,如下所示并设置值。
<TextBox Loaded="TextBox_Loaded" />
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
var txtBox = (TextBox)sender;
txtBox.Text = "Some Text";
}
但问题是,如果我在每个 HubSection
.
中对 bind/access 的控制很少,那么这样做并不好
所以有人可以告诉我是否有另一种简单的方法来绑定控件。
试试这个:
C#:
在 class 后面的代码中(例如 Page
),您需要定义包含 RootObject
的 属性。请注意,如果您想对此 属性 值的更改做出反应,您可能需要实施 INotifyPropertyChanged。
public Page1 : Page {
public RootObject RootObject { get; set; }
}
XAML:
在您的 XAML 中,您应该像这样简单地使用绑定到 RootObject
的属性:
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding RootObject.Text1}" />
</DataTemplate>
</HubSection>
<HubSection Header="Section2">
<DataTemplate>
<TextBox Text="{Binding RootObject.Text2}"/>
</DataTemplate>
</HubSection>
希望对您有所帮助
C#
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
RootObject ro = new RootObject() {Text1 = "Header1JsonData",Text2= "Header2JsonData",Text3= "Header3JsonData"};
this.DataContext = ro;
}
}
public class RootObject
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
}
XAML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub >
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text1}"/>
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text2}"/>
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text3}"/>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
输出
我正在使用如下的 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:
class RootObject
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
现在根据 this question and this 文章,我可以很好地为 TextBox
使用 Loaded
事件,如下所示并设置值。
<TextBox Loaded="TextBox_Loaded" />
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
var txtBox = (TextBox)sender;
txtBox.Text = "Some Text";
}
但问题是,如果我在每个 HubSection
.
所以有人可以告诉我是否有另一种简单的方法来绑定控件。
试试这个:
C#:
在 class 后面的代码中(例如 Page
),您需要定义包含 RootObject
的 属性。请注意,如果您想对此 属性 值的更改做出反应,您可能需要实施 INotifyPropertyChanged。
public Page1 : Page {
public RootObject RootObject { get; set; }
}
XAML:
在您的 XAML 中,您应该像这样简单地使用绑定到 RootObject
的属性:
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding RootObject.Text1}" />
</DataTemplate>
</HubSection>
<HubSection Header="Section2">
<DataTemplate>
<TextBox Text="{Binding RootObject.Text2}"/>
</DataTemplate>
</HubSection>
希望对您有所帮助
C#
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
RootObject ro = new RootObject() {Text1 = "Header1JsonData",Text2= "Header2JsonData",Text3= "Header3JsonData"};
this.DataContext = ro;
}
}
public class RootObject
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
}
XAML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub >
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text1}"/>
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text2}"/>
</DataTemplate>
</HubSection>
<HubSection Header="Section1">
<DataTemplate>
<TextBox Text="{Binding Text3}"/>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
输出