Winphone开发之数据绑定(3)

本文介绍如何通过C#代码自定义数据绑定源,在Windows Phone应用中初步构造数据绑定流程。通过示例展示了如何使用XAML和CS文件实现数据与UI组件之间的动态关联,以及如何在事件触发时更新UI显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇文章主要介绍怎么自定义Binding的源,通过CS代码指定Binding的源来初步构造整个数据绑定的流程。

下面是要绑定的类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BindingLearning
{
    public class Person : INotifyPropertyChanged  
    {
        private String name;
        public event PropertyChangedEventHandler PropertyChanged;  
        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }
}

可以看到该类继承了INotifyPropertyChanged接口,然后声明了事件PropertyChanged,其中PropertyChangedEventHandler会当属性改变的时候抛出事件来通知UI,其中指明了属性名称为Name。

综上,该类会在Name属性被设置的时候发送数据更改的消息。

下面是XAML:

<phone:PhoneApplicationPage
    x:Class="BindingLearning.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="480">
        <Button Name="bt" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="178,470,0,0" Click="bt_Click"/>
        <TextBox Name="tb" HorizontalAlignment="Left" Height="72" Margin="14,222,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456"/>
    </Grid>



</phone:PhoneApplicationPage>

只是简单声明了一个Button和一个TextBox

重要的是隐藏的CS文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using BindingLearning.Resources;
using System.Windows.Data;

namespace BindingLearning
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Person p;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            p = new Person();
            Binding binding = new Binding();
            binding.Source = p;
            binding.Path = new PropertyPath("Name");
            this.tb.SetBinding(TextBox.TextProperty, binding);

        }

        private void bt_Click(object sender, RoutedEventArgs e)
        {
            p.Name += "hello";
        }

        
    }
}

构造了Person的实例p作为数据的源,然后制定里面的Name参数作为Path(Person里面有Name属性,并且我们刚刚也为它设置了监听),最后用SetBinding使得TextBox的TextProperty依赖属性和刚刚构造好的binding关联起来,最后的效果是,当p的Name属性被设置的时候,TextBox的Text依赖属性也会跟随改变。

然后我们监听了Button的点击事件,每点击一次就改变一下p的Name属性。

所以上面代码总体效果:

每当我点击一次Button,TextBox就会在原来的基础上不断地append字符串hello。


实例二:

和上面的实例差不多,但是用了DataContext,没有了Path

<phone:PhoneApplicationPage
    x:Class="PhoneApp6.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"/>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Name="Tb" Text="{Binding Name}" HorizontalAlignment="Left" Height="72" Margin="0,241,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456"/>

        </Grid>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,422,0,0" Grid.Row="1" Click="Button_Click"/>

        
    </Grid>

</phone:PhoneApplicationPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp6.Resources;

namespace PhoneApp6
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Person p;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            p = new Person() { Name = "Sirius" };
            Tb.DataContext = p;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            p.Name = DateTime.Now.Millisecond.ToString();
        }
        
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhoneApp6
{
    public class Person : INotifyPropertyChanged
    {
        private String _name;
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值