Use ModelViewDetailBase on a UserControl in a Tab

Jul 30, 2014 at 8:48 PM
Edited Jul 31, 2014 at 7:22 PM
I am working on a WPF windows app and the requirement is to have a couple of tabs that allow for the editing of items (as opposed to a pop-up window). I created a TabViewModel that exposes the Property 'SelectedTab'. When a tab is selected, if it is one of the tabs that is to be edited I tried setting the BeginEdit() like so:
        public object SelectedTabViewModel 
        {
            get
        {
            if (this._SelectedTabViewModel == null)
            {
                this.SelectedTabViewModel = Tabs[0];
            }

            if (_SelectedTabViewModel is SystemSetupViewModel)
            {
                ((SystemSetupViewModel)_SelectedTabViewModel).BeginEdit();
            }
            
            return _SelectedTabViewModel;
        }
            set
            {
                if (this._SelectedTabViewModel != value)
                {
                    this._SelectedTabViewModel = value;
                    NotifyPropertyChanged(m => m.SelectedTabViewModel);
                }
            }
        }
        private object _SelectedTabViewModel;
My SystemSetupViewModel is setup like so:
    public class SystemSetupViewModel : ViewModelDetailBase<SystemSetupViewModel, MyConfigModel>, ITabViewModel
    {
        // Default ctor
        // Use the SystemSetupViewModel(model) constructor to make all the new objects
        public SystemSetupViewModel() 
            : this ((new ViewModelLocator()).MainPageViewModel.MyConfigInfo)
        {
        }

        // TODO: Ctor to set base.Model to MyConfigModel
        public SystemSetupViewModel(MyConfigModel model)
        {
            base.Model = model;
        }
}
I am using a UserControl for the view and adding the view to a TabControl like so:
    <TabControl ItemsSource="{Binding Tabs}"
                SelectedItem="{Binding SelectedTabViewModel}">
        
        <TabControl.Resources>
            <DataTemplate DataType="{x:Type vm:RateTablesViewModel}">
                <v:RateTablesUserControl />
            </DataTemplate>
            <DataTemplate DataType="{x:Type vm:SystemSetupViewModel}">
                <v:SystemSetupUserControl />
            </DataTemplate>
        </TabControl.Resources>

        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Header}" />
                <Setter Property="Width" Value="120" />
            </Style>

        </TabControl.ItemContainerStyle>

    </TabControl>
While this functionally works (the info is displayed in the tab), the SystemSetupViewModel is not in edit mode (IsEditing is false according to Snoop). Is it possible to do this and if so can you give a small example?


EDIT
I stepped through this code into the ModelExtensions method Clone<T> and found that the following line is failing(?):
        public static T Clone<T>(this T obj)
        {
            T copy;
            using (var stream = new MemoryStream())
            {
                var ser = new DataContractSerializer(typeof(T));
                ser.WriteObject(stream, obj);                 // fails on this line
                stream.Position = 0;
                copy = (T)ser.ReadObject(stream);
            }
            return copy;
        }
When I put a break point in at the line in question above and then try to step into the WriteObject method, the program immediately returns to the line following InitailizeComponent(); in my TabsUserControl.xaml.cs file:
    public partial class TabsUserControl : UserControl
    {
        public TabsUserControl()
        {
            InitializeComponent();
        }
    }
The Clone() method doesn't return 'copy' and the lines within BeginEdit() following Copy = Model.Clone(); (line 193) are not executed.