Wpf MVVM:为每一个模块化部件提供它';s自己的XAML类

Wpf MVVM:为每一个模块化部件提供它';s自己的XAML类,wpf,xaml,user-controls,Wpf,Xaml,User Controls,我正在考虑这样做,而不是定义大量的数据模板。这意味着,如果我有一组东西,ItemsControl本身就会有一个XAML类,而对象也会有一个 当对象是包含模型和逻辑的适当ViewModels时,这已经发生了,但如果它只是一个命令,例如。可能是一组动态命令 优点:我可以使用设计师来帮助我定义对象的外观,因为我没有混合,如果需要,可以更容易地找到和更改这些部分 缺点:更多的XAML类 你能说服我做这件事还是不做这件事 示例 我在应用程序周围都有按钮,所以我定义了一个ButtonViewModel,它有

我正在考虑这样做,而不是定义大量的数据模板。这意味着,如果我有一组东西,ItemsControl本身就会有一个XAML类,而对象也会有一个

当对象是包含模型和逻辑的适当ViewModels时,这已经发生了,但如果它只是一个命令,例如。可能是一组动态命令

优点:我可以使用设计师来帮助我定义对象的外观,因为我没有混合,如果需要,可以更容易地找到和更改这些部分

缺点:更多的XAML类

你能说服我做这件事还是不做这件事

示例

我在应用程序周围都有按钮,所以我定义了一个ButtonViewModel,它有一个显示名称和一个ICommand属性。我还将为此对象定义一个DataTemplate或UserControl,它基本上是一个按钮,具有命令绑定和文本/内容绑定到显示名称。我还可以定义它的外观等等

然后,在应该包含按钮的ViewModels中,我将添加这些按钮作为类的一部分,并将它们绑定到视图中

public class ButtonViewModel : ViewModelBase
{
    private string _displayName;
    public string DisplayName
    {
        get
        { 
            return _displayName;
        }
        set
        {
            _displayName = value;
            RaisePropertyChanged("DisplayName");
        }
    }

    private ICommand _command;
    public ICommand command
    {
        get
        {
            return _command;
        }
        protected set
        {
            _command = value;
            RaisePropertyChanged("Command");
        }
    }

    public ButtonViewModel(ICommand command, string displayName)
    {
        Command = command;
        DisplayName = displayName;
    }

}
使用按钮ViewModel查看模型

public class SomeViewModel : ViewModelBase
{
    //some functionality

    //It could be done as a collection or just seperate ButtonViewModel properties
    public ObservableCollection<ButtonViewModel> Buttons { get; set; }

    //Somewhere where it makes sense, here in the constructer for the heck of it
    public SomeViewModel()
    {
        Buttons.Add(new ButtonViewModel(new RelayCommand(Save, canSave), "Save"));
        Buttons.Add(new ButtonViewModel(new RelayCommand(Edit, canEdit), "Edit"));
        Buttons.Add(new ButtonViewModel(new RelayCommand(New, canAddNew), "New"));
    }
}
公共类SomeViewModel:ViewModelBase
{
//一些功能
//它可以作为一个集合来完成,也可以只是分离ButtonViewModel属性
公共可观察收集按钮{get;set;}
//在某个有意义的地方,在构造器中
公共视图模型()
{
添加(新按钮视图模型(新RelayCommand(Save,canSave),“Save”);
添加(新按钮视图模型(新RelayCommand(Edit,canEdit),“Edit”);
按钮。添加(新按钮视图模型(新RelayCommand(new,canAddNew),“new”);
}
}
按钮视图:

<UserControl x:Class="WpfApplication1.ButtonView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="60" Width="90">
    <Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}">
        <!-- Some really cool design for your button -->

    </Button>
</UserControl>

您还可以定义一个特定的ItemsControl来保存一组按钮,甚至可以为所述ItemsControl定义一个ViewModel


我曾经了解到,如果你能在一个类中封装某个项,你应该。这只是一种疯狂的说法吗?

我不太清楚你在问什么,但听起来你好像是在采用一种视图优先的方法,除了最简单的应用程序之外,其他应用程序都会变得非常复杂。您是否考虑过使用MVVM框架,例如

使用视图模型优先的方法,可以实例化视图模型,然后使用Caliburn.Micro(通过约定)定位视图,并自动将两者绑定起来

Caliburn.Micro还将进行视图合成,因此,例如,如果您的父视图模型上有一个视图模型集合,并且您从与视图上的列表框同名的属性中公开了该集合,则Caliburn.Micro将自动为集合中的每个项使用相应的视图,并将每个items视图模型与视图绑定

public class ButtonViewModel : ViewModelBase
{
    private string _displayName;
    public string DisplayName
    {
        get
        { 
            return _displayName;
        }
        set
        {
            _displayName = value;
            RaisePropertyChanged("DisplayName");
        }
    }

    private ICommand _command;
    public ICommand command
    {
        get
        {
            return _command;
        }
        protected set
        {
            _command = value;
            RaisePropertyChanged("Command");
        }
    }

    public ButtonViewModel(ICommand command, string displayName)
    {
        Command = command;
        DisplayName = displayName;
    }

}

您也可以在同一视图模型上使用不同的视图,操作用于从视图控件中调用视图模型上的动词,而不是命令,这样可以更丰富地想象UI。

感谢您的输入,但我使用的是MVVM,而不是先执行视图。我将添加一个例子来更好地说明我的观点。