Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top