Winphone開發之數據綁定(3)

這篇文章主要介紹怎麼自定義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;
    }
}


發佈了85 篇原創文章 · 獲贊 2 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章