使用UserControl混淆

时间:2014-01-29 22:28:03

标签: .net wpf mvvm user-controls

我已经使用viewmodelocator应用了wpf mvvm。

我希望我可以创建一个通用的usercontrol来做crud。

此用户控件,可以在多个视图中使用。

我的观点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>

我的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();

    }

}

然后我的用户控制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>

我的用户控制视图模型

  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();

    }
}

我的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();
        }
    }
}

实施例: 查看Articoli: enter image description here

查看CLienti: enter image description here

和我的示例codesource:BrunoMvvMLocator

Crudusercontrol(我的usercontrol)不知道视图的模型及其所有更改。 我能怎么做? 或者还有其他方法??

1 个答案:

答案 0 :(得分:1)

药膏!如果我不正确理解这个问题,你将不得不原谅我,但看起来你正试图将当前的视图模型传递给其中一个保存命令处理程序。您在此处发布的代码只有一个按钮,但您发布的演示代码有两个,所以我只会告诉您如何为每个代码执行此操作。这两种情况都可以通过在Command CommandParameter中传递视图模型来实现,在第一种情况下执行此操作:

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

将使用类型为CrudUserControlViewModel<Articoli>的对象调用命令处理程序。在第二种情况下这样做:

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

将使用Articoli类型的对象调用命令处理程序。

另一件事,我注意到你正在调用OnPropertyChanged(),你应该真正传递正在更新的属性的名称,即:

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

请原谅我,如果我误解了这个问题,我完全依赖谷歌翻译来理解你的代码评论:)