微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight的绑定数据(初步学习)

刚着手学习....

xmal文件代码如下:

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="600">

    <Grid Background="#46461F">
        <Grid.RowDeFinitions>
            <RowDeFinition Height="40"></RowDeFinition>
            <RowDeFinition Height="*"></RowDeFinition>
        </Grid.RowDeFinitions>
        <Grid.ColumnDeFinitions>
            <ColumnDeFinition></ColumnDeFinition>
        </Grid.ColumnDeFinitions>
        <Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
            Width="240" Height="36" Background="Orange"
            Margin="20 0 0 0" HorizontalAlignment="Left">
            <TextBlock Text="文章列表" Foreground="White"
                   HorizontalAlignment="Left" VerticalAlignment="Center"
                   Margin="20 0 0 0"></TextBlock>
        </Border>
        <ListBox x:Name="PostList" Grid.Column="0" Grid.Row="1"
             Margin="40 10 10 10"
             HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom"
             ItemsSource="{Binding Post,Mode=OneWay}">
        </ListBox>
        <Button Margin="280,260,120,0" Width="200" Height="100" Grid.Row="1" Click="Button_Clisk" Content="点击我更改数据" FontSize="22"></Button>
    </Grid>
</UserControl>
绑定数据类:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
namespace SilverlightApplication2
{
    public class Class1:INotifyPropertyChanged
    {
        private List<string> post;
        public List<string> Post {
            get {
                return post;
                }
            set {

                post = value;
                Notifity("Post");
            }
        
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void Notifity(string pro) {

            if (null != PropertyChanged)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(pro));
            }
        }
    }
}

主页代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication2;
namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        Class1 cls;
        public MainPage()
        {
            InitializeComponent();
            Load();
            PostList.DataContext = cls;
        }
        private void Load() {
            cls = new Class1();
            cls.Post = new List<String> {
            "一步一步学Silverlight:使用用户控件","一步一步学Silverlight :使用控件模板","一步一步学Silverlight :使用样式封装控件观感","一步一步学Silverlight:全屏模式支持"
            };
        }

        private void Button_Clisk(object sender,RoutedEventArgs e)
        {
            cls.Post = new List<string> { 
                 "学习怎么绑定数据:使用用户控件","学习控件的基本使用:使用控件模板","xmal的基本使用:使用样式封装控件观感","c#的学习):全屏模式支持"
            };
        }
    }
}



版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐