我试图为Windows Phone(C#/ XAML)设计一个非常基本的应用程序。起初,我使用了集线器模板,但是我迷路了所以我决定一步一步地从一个空白的应用程序开始并添加一个集线器。
我设法在两个不同的页面之间绑定数据,使用行代码,例如TextBox.DataContext = DataContext ...但是我想将数据正确地绑定到集线器部分,这不是那么容易,因为集线器元素谎言在" DataTemplate"无法访问。
我花了两周时间阅读文档,教程等等......现在我已经阅读了很多不同来源,我完全迷失了,不知道该怎么办。
这是我的应用:A"玩家"班级(球员姓名和得分);以及一个包含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的TextBox。 我的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中使用ItemsSource = {Binding} + ListBox.DataContext =后面的代码中的播放器),但是这里我只想显示一个给定元素我的球员...... 我试图添加一个xmlns:data =&#34; clr-namespace&#34;但这似乎不起作用(IDE不建议任何自动完成)
我可能在某处做错了什么......但无法弄清楚到底在哪里。如上所述,我尝试了很多不同的选项,现在我完全迷失了......
答案 0 :(得分:4)
好的,我终于发现我的代码出了什么问题。 首先,在xaml中,对于每个列表框,确保添加绑定
<ListBox x:Name="lb" ItemsSource="{Binding}" Grid.Column="1">
然后,例如,要绑定到文本框,我只需添加:
<TextBlock Text="{Binding PlayerName}" FontSize="32" Grid.Column="0"/>
在后面的代码中,一个简单的:
MainHub.DataContext = players;
就是这样,现在一切都完美无缺。谢谢你的帮助