首页 / C# / 数据绑定到C#XAML中的中心部分
数据绑定到C#XAML中的中心部分
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了数据绑定到C#XAML中的中心部分,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3901字,纯文字阅读大概需要6分钟。
内容图文
![数据绑定到C#XAML中的中心部分](/upload/InfoBanner/zyjiaocheng/681/9615854b045b492a860d311b4806d3c0.jpg)
我正在尝试为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;
就是这样,现在一切都完美地结合在一起了.谢谢你的帮助
内容总结
以上是互联网集市为您收集整理的数据绑定到C#XAML中的中心部分全部内容,希望文章能够帮你解决数据绑定到C#XAML中的中心部分所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。