当前位置:编程学习 > C#/ASP.NET >>

WPF 数据绑定到其他对象的属性

基本类的定义如下 , 有一个GameCard 类 里有一个属性Number, 而同时又一个GameCardService  是伽mecard类的集合,现在我在我的主窗体 里 定义了一个GameCardService 的实例 和属性;现在我需要再握得xmal里对一个标签的内容进行绑到Number ,我的写法是这样的 

比如第一个标签绑到到 PGameCard[0]的number  写法如下 Path = Number, Source = PGameCard[0]

想请教熟悉的人帮我看看为什么不起作用阿,我希望通过 Source的方式不想通过datacontext的方式;正确的应该是怎么写。。具体的部分代码如下。。



Public  GameCardService PGameCard
{
   get{
return pGamecard;
}set
{
   pGameCard  = value;
}
}


================================================

   public class GameCard : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void PropertyChangedNotify(string propName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        private String number;
        public String Number
        {
            set
            {
                Number = value;
            }
            get
            {
                return number;
            }
        }

        public GameCard(string _number)
        {

            number = _number;
         }

    }

    public class GameCardService : ObservableCollection<GameCard>, INotifyPropertyChanged
    {
        public GameCardService()
            : base()
        {
            for (int i = 0; i < 54; i++) Add(new GameCard(""));
            
        }

        public void setNumbers(int[] _Number)
        {
            for (int i = 0; i < 54; i++)
            {
                this[i].Number = _Number[i].ToString();
            }
        }
    } WPF 数据绑定 --------------------编程问答-------------------- 绑定, 大牛帮看看啊 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 大牛帮顶啊 ,着急  --------------------编程问答-------------------- 我用你的代码给你写个 MVVM 的demo:

首先,是两个Model类
using System;
using System.ComponentModel;

namespace WpfApplication2
{
    public class GameCard : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void PropertyChangedNotify(string propName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        private String number;
        public String Number
        {
            set
            {
                Number = value;
            }
            get
            {
                return number;
            }
        }

        public GameCard(string _number)
        {

            number = _number;
        }

    }

}

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication2
{
    public class GameCardService : ObservableCollection<GameCard>, INotifyPropertyChanged
    {
        public GameCardService()
            : base()
        {
            for (int i = 0; i < 54; i++)
                Add(new GameCard("Number" + i));

        }

        public void setNumbers(int[] _Number)
        {
            for (int i = 0; i < 54; i++)
            {
                this[i].Number = _Number[i].ToString();
            }
        }
    }
}


接下来,因为我们要使用窗体MainWindow来显示它,所以自然地,定义一个对应的VM类

namespace WpfApplication2
{
    public class MainWindowModel
    {
        public MainWindowModel()
        {
            this.PGameCard = new GameCardService();
        }

        public GameCardService PGameCard { get; set; }
    }
}


最后就是最终目的——View类
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowModel />
    </Window.DataContext>
    <Grid>
        <TextBlock Text="{Binding PGameCard[0].Number}" />
    </Grid>
</Window>


我首先会把 MainWindow.xaml.cs 文件删除。因为只有这样,才可以真正比较严格地使用 MVVM 模式。 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 4 楼 sp1234 的回复:
我用你的代码给你写个 MVVM 的demo:

首先,是两个Model类
using System;
using System.ComponentModel;

namespace WpfApplication2
{
    public class GameCard : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void PropertyChangedNotify(string propName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        private String number;
        public String Number
        {
            set
            {
                Number = value;
            }
            get
            {
                return number;
            }
        }

        public GameCard(string _number)
        {

            number = _number;
        }

    }

}

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication2
{
    public class GameCardService : ObservableCollection<GameCard>, INotifyPropertyChanged
    {
        public GameCardService()
            : base()
        {
            for (int i = 0; i < 54; i++)
                Add(new GameCard("Number" + i));

        }

        public void setNumbers(int[] _Number)
        {
            for (int i = 0; i < 54; i++)
            {
                this[i].Number = _Number[i].ToString();
            }
        }
    }
}


接下来,因为我们要使用窗体MainWindow来显示它,所以自然地,定义一个对应的VM类

namespace WpfApplication2
{
    public class MainWindowModel
    {
        public MainWindowModel()
        {
            this.PGameCard = new GameCardService();
        }

        public GameCardService PGameCard { get; set; }
    }
}


最后就是最终目的——View类
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowModel />
    </Window.DataContext>
    <Grid>
        <TextBlock Text="{Binding PGameCard[0].Number}" />
    </Grid>
</Window>


我首先会把 MainWindow.xaml.cs 文件删除。因为只有这样,才可以真正比较严格地使用 MVVM 模式。


大哥,你这样绑定的写法,好像我还是不行啊,没法显示唉 --------------------编程问答--------------------

前台
 <ListView  x:Name="listview">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Number}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
后台
listview.ItemsSource = GameCardService;
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,