WPF更新Datagrid绑定到ObservableCollection [英] WPF Update Datagrid Binding To ObservableCollection

查看:279
本文介绍了WPF更新Datagrid绑定到ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我知道这是一个常规问题,但我对我的程序的行为感到不满。我想将自定义对象的observablecollection绑定到datagrid。当通过代码完成绑定时,在通过wcf接收列表后,datagrid显示itemssource。但是当我绑定xaml时,这不起作用。我想,可观察集合会自动更新collectionchangedevent上的datagrid。这是我的代码,附有一些评论:



Hi together,

i know that this is kind of a regular question but i am konfused on the behavior of my programm. I want to bind an observablecollection of custom objects to a datagrid. When the binding is done via code, after receiving the list over wcf, the datagrid shows the itemssource. But this doesn''t work when i bind over xaml. i thought, that the observable collection automaticaly updates the datagrid over the collectionchangedevent. Here is my Code with a few comments:

public partial class AddressPage : Page
    {
        ObservableCollection<Address> AddressCollection =
        new ObservableCollection<Address>();
    
        public AddressPage()
        {
            this.LoadAddress();
            InitializeComponent();
        }

        private async void LoadAddress()
        {
            RestServiceClient client = new RestServiceClient();
            AddressCollection = await client.GetAddressAsync();
            MainGrid.ItemsSource = AddressCollection; //this works but i dont want it this way...
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            AddressCollection.Add(
            new Address { address1 = "test1", address2 = "test2" });
        }
    }





这是XAML代码。 ItemsSource ={Binding AddressCollection}无效...





This is the XAML Code. The ItemsSource="{Binding AddressCollection}" is not working...

DataContext="{Binding RelativeSource={RelativeSource Self}}"
Style="{StaticResource standardDataPage}"
d:DesignHeight="300" d:DesignWidth="300"
Title="AddressPage">




<StackPanel>
<DataGrid x:Name="MainGrid" ItemsSource="{Binding AddressCollection}"  AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="address1" Binding="{Binding address1}"/>





除了几篇文章,我还阅读了comarison-articel:

List vs ObservableCollection vs I Silverlight中的NotifyPropertyChanged [ ^ ]

和tutroial:

WPF DataGrid实际示例 [ ^ ]

但我无法得到它......



Beside a few posts i read the comarison-articel:
List vs ObservableCollection vs INotifyPropertyChanged in Silverlight[^]
and the tutroial:
WPF DataGrid Practical Examples[^]
But i cant''t get it...

推荐答案





我想,可观察的集合会自动更新collectionchangedevent上的数据网格 - 这是第一次混淆

这个事件会在一个案例中引发你(在这个集合中添加,删除..等数据)。

所以
Hi,

" i thought, that the observable collection automaticaly updates the datagrid over the collectionchangedevent" -this is a first confusion
this event will fire in oly one case when you (add, delete .. etc data whithin this collection).
So
MainGrid.ItemsSource = AddressCollection;

并不会激活这个事件! !



如果你想要所有这些东西自动工作你需要为你的数据提供MVVM模式,例如:

doesn''t fire the event!!

If you want all this stuff work automatically withot any code like below, you need, to provide MVVM pattern to your Data

for example:

public class MainViewModel
{
private ObservableCollection<address> _addressCollection;
public ObservableCollection<address> AddressCollection
{
  get{ return _addressCollection;}
  set{ _addressCollection=value; RaiseProperchyChanged("AddressCollection")}
} 
}
</address></address>




<datagrid x:name="MainGrid" itemssource="{Binding AddressCollection}" datacontext="{Binding" mode="hold" xmlns:x="#unknown" />


这篇关于WPF更新Datagrid绑定到ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆