【问题标题】:How to access controls inside HubSection in Windows 8.1如何在 Windows 8.1 中访问 HubSection 内的控件
【发布时间】: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 属性绑定到RootObjectText1,第二个绑定到Text2 等等?你想用RootObject 来填充文本框还是用其他方式?

标签: c# json xaml windows-store-apps


【解决方案1】:

希望对你有帮助

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>

输出

【讨论】:

    【解决方案2】:

    试试这个:

    C#:

    在类后面的代码(例如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>
    

    【讨论】:

      猜你喜欢
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      相关资源
      最近更新 更多