【问题标题】:Confusion using UserControl使用 UserControl 的困惑
【发布时间】:2014-02-22 00:29:26
【问题描述】:

我有应用 wpf mvvm 和 viewmodelocator。

我希望我可以创建一个通用的用户控件来完成这项工作。

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

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

我的视图模型 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:

查看客户:

和我的示例代码源:BrunoMvvMLocator

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

【问题讨论】:

  • 虽然我知道英语不是您的母语,但您目前问题中的文字很难理解。如果您希望有人回答您的问题,您能否尝试通过清楚地解释您的问题来改进它?
  • 你说得对,英语不是我的母语,现在我变了,希望你能理解得更好

标签: .net wpf mvvm user-controls


【解决方案1】:

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

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

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

<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
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-04-16
    • 2011-01-26
    • 2013-04-04
    • 2015-07-19
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多