数据绑定到 C# 中的集线器部分 XAML

Data binding to hub section in C# XAML

我正在尝试为 windows phone (C#/XAML) 设计一个非常基本的应用程序。起初,我使用的是 hub 模板,但我迷路了,所以我决定一步一步从一个空白的应用程序开始,添加一个 hub。

我设法在两个不同的页面之间绑定数据,行代码如 TextBox.DataContext = DataContext ...但我想将数据正确绑定到集线器部分,这并不容易,因为集线器元素位于 "DataTemplate" 中,无法访问。

过去两周我一直在阅读文档、教程等...现在我阅读了很多不同的资源,以至于我完全迷失了方向,不知道我应该做什么。

这是我的应用程序:A "Players" class(玩家姓名和分数);以及一个包含 2 个部分的集线器控件的简单页面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App1
{
class Players
{
    private string playerName;
    public string PlayerName
    {
        get { return playerName; }
        set { playerName = value; }
    }
    private int playerScore;
    public int PlayerScore
    {
        get { return playerScore; }
        set { playerScore = value; }
    }                
}
}

后面的代码非常基本,我刚刚创建了一个列表,其中只填充了两个玩家。我现在只想了解数据绑定的工作原理。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App1
{
    public sealed partial class MainPage : Page
{
    public MainPage()
    {
        List<Players> players = new List<Players>();

        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

        Players player1 = new Players();  Players player2 = new Players();
        player1.PlayerName = "Vince"; player1.PlayerScore = 2;  player2.PlayerName = "Mike"; player2.PlayerScore = 42;
        players.Add(player1); players.Add(player2);
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }
}
}

我现在只想了解数据绑定的工作原理。 例如,我想在我的集线器中有一个显示 player1.PlayerName 的文本框。 我的 XAML 代码,截至今天看起来是:

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


<Page.Resources>
    <local:Players x:Key="PlayerDataSource"/>        
</Page.Resources>

<Page.DataContext>
    <Binding Source="{StaticResource PlayerDataSource}"/>
</Page.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <Hub x:Name="Hub1" Grid.Row="1" DataContext="Players">
        <HubSection x:Name="HubSec1">
            <DataTemplate>
                <StackPanel>
                <TextBlock Text="Hello"></TextBlock>
                <TextBox x:Name="tb1"></TextBox>
                </StackPanel>
            </DataTemplate>
        </HubSection>

        <HubSection x:Name="HubSec2">
            <DataTemplate>
                <StackPanel>
                <TextBlock Text="Section2"></TextBlock>
                <TextBlock Text="Trying to bind"/>
                    <TextBlock Text="{Binding PlayerName}"/>
                </StackPanel>
            </DataTemplate>
        </HubSection>
    </Hub>

</Grid>

我知道如何将完整列表绑定到列表框之类的元素(在 XAML + ListBox.DataContext = 代码后面的玩家中使用 ItemsSource={Binding} ),但在这里我只想显示我的玩家的一个给定元素...... 我试图添加一个 xmlns:data="clr-namespace" 但这似乎不起作用(IDE 不建议任何自动完成)

我可能在某处做错了......但无法弄清楚到底在哪里。如上所述,我尝试了很多不同的选择,现在我完全迷失了...

好的,我终于发现我的代码出了什么问题。 首先,在 xaml 中,对于每个列表框,确保添加绑定

 <ListBox x:Name="lb" ItemsSource="{Binding}" Grid.Column="1">

然后,例如,要绑定到文本框,我只需添加:

<TextBlock Text="{Binding PlayerName}" FontSize="32" Grid.Column="0"/>

在后面的代码中,一个简单的:

            MainHub.DataContext = players;

就是这样,现在一切都完美结合了。感谢您的帮助