我很难让ObjectDataProvider使用方法参数。现在使用以下代码,我可以在XAML visual studio设计器中查看数据,但是一旦我尝试刷新ObjectDataProvider,我的数据就会消失。我已经设置了一个带有五个参数的对象数据提供程序,虽然它们没有传递给数据集查询,我只是想看看我是否可以先传递这些参数。我在底部运行filter方法刷新OBJDataProvider并且我的datagrid变为空白?
我的两个ObjectDataProviders
<ObjectDataProvider x:Key="LogDataProvider" ObjectType="{x:Type local:LogDataProvider}"/>
<ObjectDataProvider x:Key="siteListContains" MethodName="GetLog2" ObjectType="{x:Type local:LogDataProvider}">
<ObjectDataProvider.MethodParameters>
<s:String>Red</s:String>
<s:DateTime>12:14:24</s:DateTime>
<s:DateTime>2008-01-01</s:DateTime>
<s:DateTime>12:12:12</s:DateTime>
<s:DateTime>2008-01-01</s:DateTime>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
数据网格及其与对象数据提供程序的绑定
<dg:DataGrid Name="log_datagrid" Height="230" VerticalAlignment="Stretch"
DataContext="{Binding Source={StaticResource siteListContains}}"
ItemsSource="{Binding}" AutoGenerateColumns="True"/>
我的LogDataProvider类
public class LogDataProvider
{
private eventlogTableAdapter adapter;
DataSet1 dataset;
public LogDataProvider()
{
dataset = new DataSet1();
adapter = new eventlogTableAdapter();
}
public DataView GetLog2(string sitestr, DateTime fromtimestr, DateTime fromdatestr, DateTime totimestr, DateTime todatestr)
{
adapter.Fill(dataset.eventlog);
return dataset.eventlog.DefaultView;
}
}
这里我遇到了问题,当我将sitestr传递给MethodParameter [0]时,它会立即跳转到LogDataProvider的GetLog2方法,而不会接收其余的参数......然后我的数据网格变为空白: - (
private void filter_btn_Click(object sender, RoutedEventArgs e)
{
ObjectDataProvider sitelist_dataobj = this.FindResource("siteListContains") as ObjectDataProvider;
sitelist_dataobj.MethodParameters[0] = sitestr.Text.ToString();
sitelist_dataobj.MethodParameters[1] = from_timePicker.SelectedTime;
sitelist_dataobj.MethodParameters[2] = from_datepicker.SelectedDate;
sitelist_dataobj.MethodParameters[3] = to_timePicker.SelectedTime;
sitelist_dataobj.MethodParameters[4] = to_datepicker.SelectedDate;
sitelist_dataobj.Refresh();
}
答案 0 :(得分:0)
每次更新任何参数时都应调用Refresh。如果你要设置多个,你应该使用DeferRefresh让它等到你完成:
private void filter_btn_Click(object sender, RoutedEventArgs e)
{
ObjectDataProvider sitelist_dataobj = this.FindResource("siteListContains") as ObjectDataProvider;
using (sitelist_dataobj.DeferRefresh())
{
sitelist_dataobj.MethodParameters[0] = sitestr.Text.ToString();
sitelist_dataobj.MethodParameters[1] = from_timePicker.SelectedTime;
sitelist_dataobj.MethodParameters[2] = from_datepicker.SelectedDate;
sitelist_dataobj.MethodParameters[3] = to_timePicker.SelectedTime;
sitelist_dataobj.MethodParameters[4] = to_datepicker.SelectedDate;
}
}