WPF ObjectDataProvider和ComboBox

时间:2012-04-12 18:40:23

标签: wpf objectdataprovider

我已经上网了几天试图解决这个问题,虽然我已经对ObjectDataProviders的工作原理和方式有了很多了解,但我仍然无法解决这个问题...我正在尝试我们一个ObjectDataProvider,用于访问我的viewmodel中的方法。在组合框中更改选择后,应该检查此方法以查看表单数据是否已被编辑。如果有,将询问用户是否要在选择更改之前保存编辑的信息。我似乎无法将两者结合在一起 - 组合框和方法的列表......我可以使组合框工作,但只有我指定ItemsSource和SelectedItem逐字。这些值是我加载其余表单信息的基础。如果你不能说,我是新手,这个人不会来找我。还有一个解释然后我会得到代码。我的应用程序是分层结构的 - 我有MainWindow,它调用PERListView,它调用EvalItemView。每个View都基于一个ViewModel,即MainWindow使用AppVM,PERListView使用PERListVM,而EvalItemView使用EvalItemVM。我遇到问题的组合框是在MainWindow中,而正在编辑的数据是在EvalItemView中。因此,我尝试使用ObjectDataProvider来获取AppVM中的SelectedNewPERListItem方法。此方法检查是否已进行编辑,询问用户是否希望保存更改,然后应返回ComboBox使用的列表。应该注意的是,当前作为ItemsSource在组合框中工作的是ObservableCollection。 SelectedItem(SelectedList)的类型为PERListVM。

OK ... ObjectDataProvider:

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

<Window.Resources>

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

MethodName =“SelectNewPERListItem”&gt;

        <ObjectDataProvider.MethodParameters>

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

        </ObjectDataProvider.MethodParameters>

    </ObjectDataProvider> 

</Window.Resources>

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>

AppVM视图模型中的SelectNewPERListItem方法:

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

我知道我错过了什么,但我不知道是什么......

1 个答案:

答案 0 :(得分:0)

我对ObjectDataProvider一无所知,但我会采用另一种方式。

假设MainWindow是一个WPF窗口,其余视图都是UserControls。 MainWindow ViewModel(AppVM)将具有PERListVM的属性,并且在MainWindow的XAML中,并将PERListView的DataContext设置为PERListVM属性。

ComboBox SelectedItem绑定AppVM上的属性,因此在此属性的setter中,如果表单数据已被编辑,则调用方法或检查PERListVM上的属性。

如果不清楚,请通过评论告诉我。

顺便说一下,您还应该重新考虑使用MessageBox的方法。调用MessageBox.Show()并不适合MVVM,但这是一个单独的问题。