在XAML中的DataGrid中绑定 [英] Binding in DataGrid in XAML

查看:59
本文介绍了在XAML中的DataGrid中绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释为什么该方法有效,所以弯曲添加到集合并在XAML中显示DataGrid.Ispolzuyu绑定。

Explain why the method works so crooked add to the collection and display DataGrid.Ispolzuyu binding in XAML.

<Window x:Class="bild.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:sys="clr-namespace:bild"

        Title="MainWindow" Height="519" Width="525">
    <Window.Resources>
        <sys:ViewModel x:Key="DataSource"/>        
    </Window.Resources>
 
        <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}" 

                 DisplayMemberPath="Name"                 

            HorizontalAlignment="Left" Margin="49,21,0,201" Name="listBox1" Width="425" />
        <Grid DataContext="{Binding Source={StaticResource DataSource}}" Height="149" HorizontalAlignment="Left" Margin="49,134,0,0" Name="grid1" VerticalAlignment="Top" Width="425">
            <DataGrid 

                ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}" 

                AutoGenerateColumns="False" Height="143" HorizontalAlignment="Left" Margin="36,0,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="383" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Age}"  Header="Age" ></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="296,290,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>







using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace bild
{   
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ViewModel r = new ViewModel();
            r.ad();
            dataGrid1.ItemsSource = r.Persons;
        }
    }
    public class ViewModel
    {
        public ViewModel()
        {
            this.Persons = new ObservableCollection<Person>();
            this.Persons.Add(new Person("Ivan", 23));
            this.Persons.Add(new Person("Stefan", 34));
            this.Persons.Add(new Person("Maria", 16));
            this.Persons.Add(new Person("Michael", 78));
            
        }
        public void ad()
        {
           this.Persons = new ObservableCollection<Person>();
            this.Persons.Add(new Person("Maria", 16));
            this.Persons.Add(new Person("Michael", 78));
          
        }
 
        public ObservableCollection<Person> Persons
        {
            get;
            set;
        }
    }
    public class Person
    {
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

推荐答案

我不明白你试图做什么但在add方法中。似乎你重新创建了Persons集合。

你也可以调用数据网格项源而不是列表,所以列表没有变化。

注意:你不应该'' t调用数据网格项源,但实现INotifyPropertyChanged并触发Persons集合更改的事件。



这样的事情:



公共类ViewModel:INotifyPropertyChanged

{

....



public void Ad()

{

this.Persons.Add(...

OnPropertyChanged(Persons);

}



公共事件PropertyChangedEventHandler PropertyChanged;



public void OnPropertyChanged(string propName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if(PropertyChanged!= null)PropertyChanged(this,new PropertyChangedEventHandler(propName));

}

}



这样数据网格和列表都会更新。
I don''t understand what you tried to do but in the add method. Seems you recreate the Persons collection.
Also you call the data grid items source but not the list so the list doesn''t change.
Note: You shouldn''t call the data grid items source but implement the INotifyPropertyChanged and fire an event that the Persons collection changed.

Something like this:

public class ViewModel : INotifyPropertyChanged
{
....

public void Ad()
{
this.Persons.Add(...
OnPropertyChanged("Persons");
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventHandler(propName));
}
}

This way both the data grid and the list will be updated.


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

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