WPF data mixing

I was just starting to learn WPF, and I got a little confused in some areas related to data binding. I have no problem with the syntax, but most likely some newbie errors have been made, and I have a couple of questions.

I made a simple screen with 2 text fields, and when I click the button, these two elements are added to the ListBox.

Link in the Window tag XAMLto the People class

 xmlns:classes="clr-namespace:WPF_Course.Classes"

Window resource added

<Window.Resources>
        <classes:People x:Key="people"/>
</Window.Resources>

This is how I announced my Listbox

<ListBox DataContext="{Binding Source={StaticResource people}}"
                 ItemsSource="{Binding Persons}"
                 x:Name="PersonListBox">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel>
                            <TextBlock Text="{Binding FullName}"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>

            </ListBox.ItemTemplate>                
</ListBox>

So, I added DataContexta ListBox to my list, where I bind it to the resource of my people, and also add ItemSourcethat looks at the property in mine People.

This is my class

public class People : ObservableCollection<Person> 
    {
        public ObservableCollection<Person> Persons { get { return persons; } set { persons = value; } }
        private ObservableCollection<Person> persons = new ObservableCollection<Person>();

        public People()
        {
            for (int i = 0; i < 1; i++)
            {
                // implicitly I add one item just for messing around with the constructor
                Persons.Add(new Person()
                {
                    Name = "Dummy",
                    LastName = "Dummy",
                    Age = 15
                });
            }
        }
    }

Based on what I have done so far, I have the following questions:

1) ( , ?)

ItemsSource = "{Binding Persons}"

ItemsSource = "{Binding Path = Persons }"

, ItemSource = "{Binding}" am, a People, , ? , , , , .

2) Peoples ObservableCollection<Person> ( Person ). , , (ObservableCollection<person> type ), ( ), , , ?, :

, ( , - ), ObservableCollection<myClass> , ? ( )

, , , , wpf.

2: , . , . ( , , , , )

 ObservableCollection<Person> ppl;
    public MainWindow()
        {
            InitializeComponent();
            person = new Person();
            stackPanelPerson.DataContext = person;

            people = new People();
            ppl = people.Persons;
            PersonListBox.ItemsSource = ppl;
        }

 ppl.Add(new Person() { Name = boxFirstName.Text, LastName = boxLastName.Text, Age = Int32.Parse(boxAge.Text) });

, Person (INotifyPropertyChanged) , :

 ppl.Add(new Person() { Name = person.Name, LastName = person.LastName, Age = person.Age});

!! !

+4
4

1:

, . {Binding xyz} {Binding Path=xyz}, . , , , :

{Binding ElementName=myElement, xyz}

:

{Binding ElementName=myElement, Path=xyz}

:

{Binding xyz, ElementName=myElement}

.

2:

, ​​ Property, ? .

.

MVVM.

+4

1) , {Binding Persons} {Binding Path=Persons}. .

public class ExceptionBinding : Binding
{
    public ExceptionBinding()
    {
        ValidationRules.Add(new ExceptionValidationRule());
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    }
}

{l:ExceptionBinding Path=Persons}.

2) . ObservableCollection<>, . List<>, , .

MVVM, .

+2

, ...

. , : SelectedValue, SelectedValuePath, SelectedItem DisplayMemberPath + Demo

, XAML VS. MVVM light framework . , MVVM, .

, ...:) , ...

+1
  • , . {Binding propertyName} {Binding Path=propertyName}, , - DataContext="{Binding Source={StaticResource people}}".

  • It depends. You do not need to use ObservableCollection<>it if the collection does not change after binding to it. Having created List<>, filled it, and then binding to it will work very well. But if you want to change the collection from the screen and the list of updates, you need to go toObservableCollection<>

-3
source

All Articles