Initializing a static resource from code in a C # WPF application

I have two windows WPF. The main one contains a grid attached to ObservableCollection<Person>. I can add and remove objects (people) from the list. I also have another window that I can show when I change a person.

A person has three properties: Name, LastName and Age and correctly implements INotifyPropertyChanged. In a new window, I have 3 text fields attached to a static Person resource called "person".

When I initialize a new window, I provide the Person object to the constructor, and then I want the properties of that person to appear in three text windows.

When the code below looks like this, everything works correctly:

public ModifyPerson(Person modPerson)  
{  
    // ... some code  
    Person p = this.Resources["person"] as Person;  
    p.Name = modPerson.Name;  
    p.LastName = modPerson.LastName;  
    p.Age = modPerson.Age;  
}  

However, I prefer to do it as follows:

public ModifyPerson(Person modPerson)  
{  
    // ... some code  
    this.Resources["person"] = modPerson;  
}

. ( , modPerson.

?

+3
3

- model. StaticResource , .

public ModifyPerson(Person modPerson)
{
    personToModify = modPerson;
}

private Person personToModify;

public Person PersonToModify
{
    get
    {
        return personToModify;
    }
}

XAML:

<StackPanel DataContext="{Binding PersonToModify}">
    <TextBox Text="{Binding Name, Mode=TwoWay" />
    <TextBox Text="{Binding LastName, Mode=TwoWay" />
    <TextBox Text="{Binding Age, Mode=TwoWay" />
</StackPanel>

( , )

DynamicResource StaticResource, model , Binding.

+1

in XAML:

<TextBox Text="{Binding Name}"></TextBox>
<TextBox Text="{Binding LastName}"></TextBox>
<TextBox Text="{Binding Age}"></TextBox>

in code: you need a copy to bind the UI for example

public EditPlayerWindow(PlayerBO arg)
        : this() {
        this.source =arg;
        this.view = new PlayerBO();
        arg.CopyTo(this.view);
        this.DataContext = view;
    }

CopyTo:

public void CopyTo(PlayerBO player) {
player.Id = this.Id;
player.Name = this.Name;
player.CurrentPolicyVersion = this.CurrentPolicyVersion;
player.CreatedAt = this.CreatedAt;
player.UpdatedAt = this.UpdatedAt;
player.Description = this.Description;
player.MACAddress = this.MACAddress;
player.IPAddress = this.IPAddress;
player.Policy = Policy;

}

Finally:

view.CopyTo(source);

then save the source code.

Help!

0
source

All Articles