gpt4 book ai didi

c# - 数据绑定(bind)到 C# XAML 中的集线器部分

转载 作者:行者123 更新时间:2023-11-30 15:26:28 25 4
gpt4 key购买 nike

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

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

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

这是我的应用程序:“玩家”类(玩家姓名和分数);以及一个包含 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; }
}
}
}

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

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)
{

}
}
}

现在,我只想了解数据绑定(bind)的工作原理。例如,我想在我的集线器中有一个显示 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>

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

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

最佳答案

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

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

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

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

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

            MainHub.DataContext = players;

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

关于c# - 数据绑定(bind)到 C# XAML 中的集线器部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29525224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com