MVVM event capture

To begin with, this is a very simplified example. My problem is a much larger project, so restructuring what I do will be the best way, so it’s not an option, I’m looking for something I can add to this example to solve the problem below, if possible.

I have 2 viewmodels( Personand Address).

Person.cs

public class Person : ViewModelBase
{
}

Address.cs

public class Address : ViewModelBase
{
    public Address() : base()
    {
       Model.OnModelChanged += Model_OnModelChanged;
    }

    private void Model_OnModelChanged(object sender, EventArgs e)
    {
    }        
}

As you can see, Addresswhen building is connected to the model, this is an important part!

Views for these two objects.

Person.xaml

<Style TargetType="{x:Type local:Person}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Person}">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Person"/>
                    <local:Address/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Address.xaml

   <Style TargetType="{x:Type local:Address}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Address}">
                    <TextBlock Text="Address"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

A Personview represents a view model Addresson its own. If I add 2 instances Personto the list in the main window, the view will create 2 new instances Address, it will be design and great.

, Person , , hook.

viewmodel , ?

enter image description here

enter image description here

+4
1

IDisposable OnModelChanged, , idisposable , - . Dispose.

- " ", .

0

Source: https://habr.com/ru/post/1617288/


All Articles