11-26win8 数据绑定

来源:互联网 发布:西门子运动控制软件 编辑:程序博客网 时间:2024/05/22 09:05

ListView 

***************************************.xaml中

<ListView SelectionMode="Multiple"  BorderBrush="Red" BorderThickness="2" Name="lv1" HorizontalAlignment="Left" Height="340" Margin="382,66,0,0" VerticalAlignment="Top" Width="310">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" Margin="10,10,10,10"/>-----------数据绑定
                            <TextBlock Text="{Binding Age}" Margin="10,10,10,10"/>----------数据绑定
                            <Image Source="{Binding Img}" Width="100" Height="100" Margin="10,10,10,10"/>----------数据绑定
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
                </ListView>

************************************.xaml.cs中

public sealed partial class listview : Page
    {
        List<person> p1 = new List<person>();-------------------------------定义person类

        public listview()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            p1.Add(new person() { Name = "张三姐", Age = 21, Img = "ms-appx:///images/DSC_0231.JPG" });
            p1.Add(new person() { Name = "李四哥", Age = 22, Img = "ms-appx:///images/DSC_0233.JPG" });
            p1.Add(new person() { Name = "志玲姐", Age = 21, Img = "ms-appx:///images/DSC_0249.JPG" });

            this.lv1.ItemsSource = p1;        }

知识2

****************.xaml

 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBox Name="txtname" HorizontalAlignment="Left" Margin="831,82,0,0" TextWrapping="Wrap" Text="{Binding Name,Mode=TwoWay}" VerticalAlignment="Top" Height="53" Width="163"/>
        <TextBox Name="txtage" HorizontalAlignment="Left" Margin="831,173,0,0" TextWrapping="Wrap" Text="{Binding Age,Mode=TwoWay}" VerticalAlignment="Top" Height="57" Width="163"/>
        <Button Content="修改Name" HorizontalAlignment="Left" Margin="1051,82,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.111,0.407" Height="53" Width="147" Click="Button_Click_1"/>
        <Button Content="读取Name" HorizontalAlignment="Left" Margin="1051,173,0,0" VerticalAlignment="Top" Height="57" Width="147" Click="Button_Click_2"/>

 

    </Grid>

.xaml.cs

  person p1 = new person()
        {
            Name = "刘晓飞"
        };

 protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.txtname.DataContext = p1;//上下文

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            p1.Name = "zhangdong";
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MessageDialog mes = new MessageDialog(p1.Name);
            mes.ShowAsync();
        }

 person类中

private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }
        private string img;
        public string Img
        {
            get { return img; }
            set {
                img = value;
                if (PropertyChanged != null)
                {
                PropertyChanged(this, new PropertyChangedEventArgs("Img"));
               
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

 

 

 

 

原创粉丝点击