开发者

MVVM: Giving every modular part it's own XAML class

开发者 https://www.devze.com 2023-02-12 19:06 出处:网络
I was thinking about doing this instead defining lot\'s of DataTemplates. This would mean that if I had a collection of things the ItemsControl itself would have a XAML class and the objects would hav

I was thinking about doing this instead defining lot's of DataTemplates. This would mean that if I had a collection of things the ItemsControl itself would have a XAML class and the objects would have one too.

This is something that already happens when the objects are proper ViewModels containing models and logic but if it's just a Command for example. A dynamic group of commands perhaps.

Pros: I could use the designer to help me define the look of the object as I don't have blend and it would be easier to find and change those parts if needed.

Cons: More XAML classes.

Would you talk me into this or out of this.

EXAMPLE

I have buttons all around the app so I define a ButtonViewModel which has a display name and a ICommand Property. I would also define a DataTemplate or UserControl for this object which would basically be a button with Command binding and text/content binding to the display name. I could also define it's look and such.

Then in ViewModels that should include buttons I would add these buttons as part of the class and bind to them inside the view.

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 开发者_如何学C= command;
        DisplayName = displayName;
    }

}

ViewModel using the ButtonViewModel

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"));
    }
}

The buttons view:

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

You could also define a specific ItemsControl to hold a collection of buttons, even going so far as to define a ViewModel for said itemscontrol.

I once learned that if you can encapsulate some item in a class you should. Is this just crazy talk?


I'm not quite sure what you're asking, but it sounds as if you are taking a view first approach, which can get very complex in everything but the simplest of apps. Have you considered using an MVVM framework such as Caliburn.Micro?

Using a view model first approach, you can instantiate your view model, and then use Caliburn.Micro to locate your view (via convention), and automatically bind the two up.

Caliburn.Micro will also do view composition, so for example, if you have a collection of view models on your parent view model, and you expose that collection from a property with the same name as a ListBox on your view, then Caliburn.Micro will automatically use the corresponding view for each item in the collection, and bind up each items view model with the view.

You can also use different views over the same view model, and Actions are used to invoke verbs on your view models from view controls, rather than commanding, which allows for much richer imagining of UIs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号