0
votes

I have applicaion wpf mvvm with viewmodelocator.

I wish I could create a generic usercontrol to do the crud.

This usercontrol, it could be used in several views.

my view xaml (ArticoliWindow):

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewModel="clr-namespace:BrunoMvvmLocator.ViewModel"
    xmlns:UserControl="clr-namespace:BrunoMvvmLocator.UserControl" x:Class="BrunoMvvmLocator.MainWindow"
    Title="ArticoliWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <viewModel:ViewModelLocator x:Key="Locator"  />
    </ResourceDictionary>
</Window.Resources>
<Grid DataContext="{Binding MainPage, Source={StaticResource Locator}}">

    <TextBox HorizontalAlignment="Left" Height="23" Margin="116,109,0,0" TextWrapping="Wrap" Text="{Binding ArticoliModel.Name}" VerticalAlignment="Top" Width="120"/>

    <Label Content="Descrizione" HorizontalAlignment="Left" Margin="30,106,0,0" VerticalAlignment="Top"/>


    <UserControl:CrudUserControl   HorizontalAlignment="Left" Margin="353,170,0,0" VerticalAlignment="Top" Height="47" Width="103"/>

</Grid>

my viewmodel ArticoliWindowViewModel:

  public class ArticoliWindowViewModel<T>:ViewModelBase where T : class
{
    private ArticoliModel _articoliModel;
    public ArticoliModel ArticoliModel
    {
        get { return _articoliModel; }
        set
        {
            _articoliModel = value;
            OnPropertyChanged();
        }
    }

    public SednaUnitOfWork Ctx;

    public MainWindowViewModel()
    {
        Ctx = new SednaUnitOfWork();

        ArticoliModel = new ArticoliModel();

    }

}

then my user control xaml:

<UserControl x:Class="BrunoMvvmLocator.UserControl.CrudUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:viewModel="clr-namespace:BrunoMvvmLocator.ViewModel"
         mc:Ignorable="d" Height="46.24" Width="102.631">
<UserControl.Resources>
    <ResourceDictionary>
        <viewModel:ViewModelLocator x:Key="Locator"  />
    </ResourceDictionary>
</UserControl.Resources>
<Grid DataContext="{Binding CrudUserControl, Source={StaticResource Locator}}">
    <Button Content="Salva2" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SalvaCommand}"/>

</Grid>

my user control view model

  public class CrudUserControlViewModel<T> : ViewModelBase where T : class
{
    public RelayCommand SalvaCommand { get; set; }
    public RepositoryBase<T> repository;
    public SednaUnitOfWork Ctx;
    public object _model;
    public CrudUserControlViewModel(EntityBase model, SednaUnitOfWork ctx)
    {
        _model = model;
        Ctx = ctx;
        repository = new RepositoryBase<T>(Ctx);
        SalvaCommand = new RelayCommand(Salva);
    }

    private void Salva(object _obj)
    {


        repository.Aggiungi(ArticoliModel);//should be the model of my view

        Ctx.SaveChanges();

    }
}

my ArticoliModel:

    public class ArticoliModel:EntityBase
{
    private string _codice;
    private string _descrizione;
    public string Codice
    {
        get { return _codice; }

        set
        {
            _codice = value;
            OnPropertyChanged();
        }
    }



    public string Descrizione
    {
        get { return _descrizione; }
        set
        {
            _descrizione = value;
            OnPropertyChanged();
        }
    }
}

EXAMPLE: view Articoli: enter image description here

view CLienti: enter image description here

and my example codesource:BrunoMvvMLocator

Crudusercontrol(my usercontrol) not know the model of view and all its changes. How can I do? or there are other ways??

1
While I understand that English is not your first language, the text that you have in your question currently is very difficult to understand. If you want somebody to answer your question, can you please try to improve it by clearly explaining your problem? - Sheridan
you're right, English is not my first language, now I've changed, I hope you understand better - Brux88

1 Answers

1
votes

Salve! You'll have to forgive me if I don't understand the question correctly but it looks like you are trying to pass the current view model into one of the save command handlers. The code you've posted here has only one button but the demo code you posted has two, so I'll just show you how to do it for each. Both cases can be achieved by passing the view model in via CommandParameter, in the first case do this:

<Button Content="Salva con UserControl" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="148" Command="{Binding SalvaCommand}" CommandParameter="{Binding}"/>

The command handler will be called with an object of type CrudUserControlViewModel<Articoli>. In the second case do this:

<Button Content="Salva Senza UserControl" HorizontalAlignment="Left" Margin="319,224,0,0" VerticalAlignment="Top" Width="164" Command="{Binding SalvaNormaleCommand}" CommandParameter="{Binding ArticoliModel}"/>

The command handler will be called with an object of type Articoli.

One other thing, I notice you're calling OnPropertyChanged(), you should really be passing in the name of the property being updated, i.e. this:

public string Descrizione
{
    get { return _descrizione; }
    set
    {
        _descrizione = value;
        OnPropertyChanged("Descrizione"); // <----- look here
    }
}

Please forgive me if I've misunderstood the question, I'm relying entirely on google translate to understand your code comments :)