0

I've been online for days trying to figure this one out and, while I've gotten a lot of insight into what and how ObjectDataProviders work, I am still unable to solve this one... I am trying to us an ObjectDataProvider to access a method in my viewmodel. After the selection has been changed in a combobox, this method is supposed to check to see if the form data has been edited. If it has, the user will be asked if they would like to save the edited information before the selection changes. I can't seem to tie the two together - the list for the combobox and the method... I can get the combobox working, but only if I specify ItemsSource and SelectedItem verbatim. These values are the basis for the rest of my form information being loaded. If you can't tell, I'm a newbie and this one just won't come to me. One more explanation and then I'll get to the code. My app is structured in layers - I have MainWindow, which calls PERListView, which calls EvalItemView. Each View is based on a ViewModel, i.e, MainWindow uses AppVM, PERListView uses PERListVM, and EvalItemView uses EvalItemVM. The combobox I'm having trouble with is in MainWindow while the data being edited is in EvalItemView. Thus I am trying to use the ObjectDataProvider to get ahold of the SelectedNewPERListItem method in AppVM. This method checks to see if edits have been made, asks the user if they wish to save the changes and then is supposed to return the list that is used by the ComboBox. It should be noted that what is currently working in the combobox as the ItemsSource is a ObservableCollection. And the SelectedItem (SelectedList) is of type PERListVM.

OK... The ObjectDataProvider:

xmlns:viewmodel="clr-namespace:PERTrack.ViewModel"

<Window.Resources>

    <ObjectDataProvider x:Key="PERListProvider" ObjectType="{x:Type viewmodel:AppVM}" 

MethodName="SelectNewPERListItem" >

        <ObjectDataProvider.MethodParameters>

            <sys:Int32>1</sys:Int32>

        </ObjectDataProvider.MethodParameters>

    </ObjectDataProvider> 

</Window.Resources>

The ComboBox:

SelectedItem="{Binding SelectedList}" IsSynchronizedWithCurrentItem="True" Background="WhiteSmoke" >
<ComboBox.SelectedValue>
    <Binding Source="{StaticResource PERListProvider}" BindsDirectlyToSource="True" 
      UpdateSourceTrigger="PropertyChanged" Mode="OneWay" />
</ComboBox.SelectedValue>
<ComboBox.ItemTemplate>
   <DataTemplate>
      <TextBlock Text="{Binding Path=PERList_ListID}" />
   </DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ComboBox.ItemContainerStyle>
</ComboBox>

The SelectNewPERListItem method in the AppVM viewmodel:

    private PERListVM SelectNewPERListItem(object noParam)
    {
        if (_SelectedList != null)
        {
            if (_SelectedList.SelectedItem != null)
            {
                if (_SelectedList.SelectedItem.IsDirty)
                {
                    System.Windows.Forms.DialogResult SaveEval;
                    SaveEval = System.Windows.Forms.MessageBox.Show("Do you wish to save your updates?", "User Action", System.Windows.Forms.MessageBoxButtons.YesNo);

                    // the user wants to save the updated information
                    if (SaveEval == System.Windows.Forms.DialogResult.Yes)
                    {
                        App.context.SaveChanges();
                    }
                }
            }
        }

        return _SelectedList;
    }

I know I'm missing something, but what it is I don't know...

dastokesh
  • 3
  • 3

1 Answers1

0

I don't know anything about ObjectDataProvider, but I'd approach this another way.

Let's say MainWindow is a WPF Window, the rest of your views are UserControls. The MainWindow ViewModel (AppVM) would have a property for the PERListVM, and in the XAML for the MainWindow, and set the DataContext for PERListView to the PERListVM property.

The ComboBox SelectedItem binds a property on the AppVM, so in the setter of this property, call a method or check a property on the PERListVM if the form data has been edited.

Let me know via a comment if this isn't clear.

BTW, you should also rethink your approach with the MessageBox. Calling MessageBox.Show() doesn't sit too well with MVVM, but that's a seperate problem.

Wayne Maurer
  • 12,333
  • 4
  • 33
  • 43
  • As for the us of MessageBox - I'm just learning MVVM as well as WFP. AND having to do it with time constraints... As for your other comments, I've tried doing what you've suggested, but can't figure out how to do them. I don't know how to call what I need to and, unfortunately, I'm too new at this to fully understand what I'm being told when I look at the MSDN sites... And I really need to check EvalItemView for edits... Thanks for your comments!!! – dastokesh Apr 13 '12 at 13:37
  • I appologize for what, as I re-read my comment sounds rather abrupt and does not ask for further help... Can you tell me exactly how I would call a method or check a property in the ComboBox SelectedItem setter? I've tried several different approaches and can't seem to figure it out. As for the Messagebox, how, using MVVM would I go about changing the use of the MessageBox? Thanks for any/all help you can provide!!! – dastokesh Apr 16 '12 at 12:37
  • After consulting with a co-worker I've realized that, because of my newbieness, I was trying to put the code in the wrong place!!! When you said "setter" I didn't realize the get/set of the constructors were called "getter/setters". I had thought you were referring to the form code-behind where there's a setter verb. Thanks for your help!!! This has solved a MAJOR stopping point in my application!!!! – dastokesh Apr 22 '12 at 11:27
  • Hey, sorry for not getting back to you earlier, but I'm glad I was able to help, and thanks for marking as an answer. As for MessageBox and MVVM, there are are number of ways to implement this, e.g. [Josh Smith's solution](http://www.codeproject.com/Articles/70223/Using-a-Service-Locator-to-Work-with-MessageBoxes), or many more if you google "mvvm messagebox". Something else to consider would be to use a MVVM framework such as MVVM light or Caliburn micro. – Wayne Maurer Apr 23 '12 at 08:07