编程语言
首页 > 编程语言> > 数据绑定到C#XAML中的中心部分

数据绑定到C#XAML中的中心部分

作者:互联网

我正在尝试为Windows Phone(C#/ XAML)设计一个非常基本的应用程序.最初,我使用了集线器模板,但是迷路了,所以我决定逐步进行操作,从一个空白的应用程序开始,并添加了一个集线器.

我设法用诸如TextBox.DataContext = DataContext等行代码在两个不同的页面之间绑定数据,但我想将数据正确地绑定到中心部分,这并不容易,因为中心元素位于“ 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; }
    }                
}
}

后面的代码非常基本,我只是创建了一个列表,其中仅填充了两个播放器.目前,我要做的只是了解数据绑定的工作方式.

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 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;

就是这样,现在一切都完美地结合在一起了.谢谢你的帮助

标签:data-binding,windows-phone-8-1,wpf,xaml,c
来源: https://codeday.me/bug/20191028/1952799.html