首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,这个绑定为何没有效果

2013-09-06 
WPF,这个绑定为什么没有效果Window x:ClassWPF熊俊.MainWindowxmlnshttp://schemas.microsoft.com/w

WPF,这个绑定为什么没有效果


<Window x:Class="WPF熊俊.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Name="window1" Height="399.259" Width="604.074">
    <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
        <ListBox ItemsSource="{Binding ElementName=window1, Path=ListPerson}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
    </Grid>
</Window>



public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ListPerson = new List<Person>() 
        {
            new Person(){Name="张三",Adress="南街"}, 
            new Person(){Name="李四",Adress="北京"},
            new Person(){Name="王二",Adress="上海"},
        };
    }
    private  List<Person> listperson;
    public List<Person> ListPerson
    {
        get { return listperson; }
        set { listperson = value; } 
    }
}

public class Person
{
    public string Name { get; set; }
    public string Adress { get; set; }
}


上面的代码中,将窗口的Name属性设为window1,在XAML标签中,将ListBox的ItemsSource绑定到窗口,然后指定ListPerson属性,可是,结果为什么没有效果呢?哪里不正确呢?


[解决办法]
因为你写的绑定是在 InitializeComponent()时候执行的,这时候ListPerson为空,所以没有显示任何数据。
把初始化顺序换下就可以了:
ListPerson = new List<Person>() { 。。。 };
InitializeComponent();

或者把ListPerson做成DependencyProperty,这样ListPerson变了以后才能通知ui作出反应。
[解决办法]

引用:
因为你写的绑定是在 InitializeComponent()时候执行的,这时候ListPerson为空,所以没有显示任何数据。
把初始化顺序换下就可以了:
ListPerson = new List<Person>() { 。。。 };
InitializeComponent();

或者把ListPerson做成DependencyProperty,这样ListPerson变了以后才能通知ui作出反应。


这个才是真正回答了楼主的问题。

我借此再说几句:
1. 无特殊要求的话,用ViewModel来绑定。
2. ListPerson——没有这么命名的,名称应该言简意赅,这里用People更合理。
[解决办法]
不知道啊,抱什么错,有错误信息吗?
我发现你有时候测试的内容都是改过的,用下面的代码测试下吧:

<Window x:Class="WPF熊俊.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Name="window1" Height="399.259" Width="604.074">
    <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
        <ListBox ItemsSource="{Binding ElementName=window1, Path=ListPerson}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
    </Grid>
</Window>

using System.Collections.Generic;
using System.Windows;

namespace WPF熊俊
{
public partial class MainWindow : Window
{
public MainWindow()
{
ListPerson = new List<Person>()
{
new Person() {Name = "张三", Adress = "南街"},
new Person() {Name = "李四", Adress = "北京"},
new Person() {Name = "王二", Adress = "上海"},
};
InitializeComponent();
}

private List<Person> listperson;
public List<Person> ListPerson
{
get { return listperson; }
set { listperson = value; }


}
}

public class Person
{
public string Name { get; set; }
public string Adress { get; set; }
}
}


[解决办法]
这时运行结果:
WPF,这个绑定为何没有效果
你那里运行看到的是什么?
[解决办法]
DependencyProperty,要写成依赖属性的.

[解决办法]
 private DependencyProperty ListPersonProperty;
        public Window1()
        {
            ListPersonProperty = DependencyProperty.Register("ListPerson", typeof(List<Person>), typeof(Window1), new PropertyMetadata());
            InitializeComponent();
            this.ListPerson = new List<Person>() { new Person() { Name = "dasdada" } };
        }

        public List<Person> ListPerson
        {
            get { return (List<Person>)GetValue(ListPersonProperty); }
            set
            {
                SetValue(ListPersonProperty, value);
            }
        }
    }

    public class Person
    {
        public string Name { get; set; }
    }

[解决办法]
你把你的测试项目打个包找个云盘传上去,我看下。
[解决办法]
主要是附加属性的问题,我以前也遇见过,这个就是附加属性一般属性的区别,附加属性实例化在类实例化之前,一般属性在类实例化之后
你这样绑定时 一般属性还没有实例化赋值,你可以使用一个Ob开头集合(可变集合),修改,你发现后面才会改变,
这样使用
看这个
http://silverlightchina.net/html/tips/2011/0520/7769.html
把这ListPerson变成附加属性就好了
------解决方案--------------------


使用注意点
WPF,这个绑定为何没有效果
[解决办法]
XAML还要加
DataContext="{Binding RelativeSource={RelativeSource Self}}“

然后直接绑定 dataSoruce={bind 你注册的属性名字我上面的是(Source),你的是啥就是啥}
[解决办法]
先执行后台程序。在InitializeComponent()方法中,用LoadComponent加载对应的xaml数据流:
Application.LoadComponent(this, new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative));

热点排行