2

There are 3 UserControls under a MainWindow. Each control have it's own Save Button. The Mainwindow has a SaveAll button.

The MainWindow has a ContentControl and the content property is binded to the VM. At runtime on ButtonClickCommand, the View is instantiated and assigned to the Content Property.

This SaveAll button will internally call methods associated with UserControls Save button. As such, SaveAll doesn't have it's own Method.

This has to be implemented by DependencyProperty.

I had once seen this scenario implemented in a Business App, but somehow missed the concept behind it.

I can't get what was the logic behind this, but it's a very useful thing.

Now I have to implement this, but i'm missing a small thing, I dont know.

I hope the scenario is clear.

Please help me in this scenario, with code.

Thanks, VJ

Varun Jain
  • 1,015
  • 2
  • 13
  • 19
  • 1
    So far nobody understands why this "has to be implemented by DependencyProperty". Perhaps you should tell use more about how your UserControls actually save, and explain why you are determined to somehow use DependencyProperties. – Joel B Fant May 18 '11 at 16:23
  • @Joel: I have updated the query here. I hope so, this would clear your confusion. If more thing can be updated, do tell me. – Varun Jain May 20 '11 at 06:19
  • No, it really doesn't. Your addition doesn't address anything I mentioned. – Joel B Fant May 20 '11 at 06:40

2 Answers2

1

Since you mentioned MVVM, here's what you might be looking for. Mind you, this will be a lot cleaner and easier if you use an MVVM framework such as Caliburn, but for this sample, its just vanilla MVVM:

public class MainViewModel
{
    public MainViewModel()
    {
        ViewOneModel = new SubViewModel();
        ViewTwoModel = new SubViewModel();
        Children = new List<SubViewModel>(new[] { ViewOneModel, ViewTwoModel });
    }

    public void SaveAll()
    {
        foreach(var child in Children)
        {
            child.Save();
        }
    }

    public IList<SubViewModel> Children { get; private set; }

    public SubViewModel ViewOneModel { get; set; }
    public SubViewModel ViewTwoModel { get; set; }
}

public class SubViewModel
{
    public void Save()
    {
    }
}

and on the UI you basically have subviews (UserControls) composed in your main view:

<StackPanel>
    <Button Width="100" Height="20" Content="Save All" />

    <local:ViewOne DataContext="{Binding ViewOneModel}" />

    <local:ViewTwo DataContext="{Binding ViewTwoModel}" />
</StackPanel>

You just need to bind the save methods to your buttons using an ICommand interface (preferably RelayCommand instance).

Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65
  • This is not solution i'm look for. My implementation is something like this. MainVM has a DependencyProperty, all other sub-VMs have a DependencyProperty. When MainVM's SaveAll is called, it'll indirectly call all the savemethod of sub views. – Varun Jain May 18 '11 at 09:37
  • @Varun: Except for this mysteriously vague DP you keep mentioning but won't explain, your description of your implementation is exactly what Hadi demonstrated. – Joel B Fant May 20 '11 at 06:38
  • Interestingly, I think it would not be such a good idea to have a DependencyProperty in a ViewModel. Isn't that supposed to be in the View? – Hadi Eskandari May 20 '11 at 09:01
  • I hope taking a DP in VM can be a suitable? What say? In VM, a DP is assigned a value and is binded to DP of control in View. Isn't a good thing? – Varun Jain May 20 '11 at 11:34
0

Imho in this scenario there is no need for RoutedEvents. The way I would solve it: There is a Main-ViewModel that exposes 3 properties with the Sub-ViewModels. The MainViewModel is the Datacontext for the window, and the subviewmodels bound to the datacontext of the 3 usercontrols.

The sub vm's are exposing a property with a Save-Command. This command is bound to the save buttons in the usercontrols. The main vm is exposing a property with a saveall-command, which is bound to the SaveAll button. In the handler of the save all command you are then iterating over the sub-vm's and call save on them.

Martin Moser
  • 6,219
  • 1
  • 27
  • 41
  • I hope so, you can catching my point. So, please be more specific or provide a code snippet. So, that I can get the workaround for it. Thanks. – Varun Jain May 18 '11 at 09:41