正在发送DataGrid(DataGridRowDetailsEventArgs e)AsyncResult
本文关键字:DataGridRowDetailsEventArgs AsyncResult DataGrid | 更新日期: 2023-09-27 18:28:13
您好。有一个DataGrid(operationGrid)RowDetailsTemplate,其中包含DataGrid(sumOperationGrid)
<DataGrid AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=OperCountUnits}" Name="operationGrid" RowDetailsVisibilityMode="VisibleWhenSelected" SelectionMode="Single" VerticalContentAlignment="Bottom" Grid.ColumnSpan="2" Margin="6,60,7,6" LoadingRowDetails="operationGrid_LoadingRowDetails" SelectionChanged="operationGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Id" Binding="{Binding Path=Id}" Header="{Binding NumberOfOperation, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="StartedAt" Binding="{Binding Path=StartedAt, Converter={StaticResource ConvertCultureDate}}" Header="{Binding StartedAt, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="FinisedAt" Binding="{Binding Path=FinishedAt, Converter={StaticResource ConvertCultureDate}}" Header="{Binding FinishedAt, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="CountMode" Binding="{Binding Path=Description}" Header="{Binding CountMode, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="CurrencyCode" Header="{Binding CurrcencyCode, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="UnitId" Binding="{Binding Path=UnitId}" Header="UnitId" IsReadOnly="True" Visibility="Collapsed" />
<DataGridTextColumn x:Name="CountModeId" Binding="{Binding Path=CountModeId}" Header="CountModeId" IsReadOnly="True" Visibility="Collapsed" />
<DataGridTextColumn x:Name="HotListCount" Binding="{Binding Path=HotListCount}" Header="Фальш" IsReadOnly="True" Visibility="Collapsed" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding CurrencySum}" Name="sumOperationGrid" SelectionChanged="sumOperationGrid_SelectionChanged" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding csId}" Visibility="Collapsed" />
<DataGridTextColumn Binding="{Binding CurrId}" Visibility="Collapsed" />
<DataGridTextColumn Binding="{Binding Curr}" Header="{Binding Currency, Source={StaticResource resources}}" />
<DataGridTextColumn Binding="{Binding TotalSum, Converter={StaticResource replaceNull}}" Header="{Binding Sum, Source={StaticResource resources}}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
请求是针对WCF服务的,结果是一切都很好如果您通过委托提交数据,那么答案在数据网格(sumOperationGrid)中并显示。
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentItem((int)_con.Id,
new AsyncCallback(
delegate(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentItem(result).ToList<CurrencySum>();
if (sum != null)
{
s2.Dispatcher.BeginInvoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[2] {s2, sum});
}
}
), null);
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
尝试过了。
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentitem((int)_con.Id, new AsyncCallback (GetCurrencySumByCurrentItemCallBack), null);
}
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
DataGridRow opGrRow = (DataGridRow)(this.operationGrid.ItemContainergenerator.containerfromitem (this.operationGrid.Items.Currentitem));
ContentPresenter cpOpGrRow = FindVisualChild<ContentPresenter>(opGrRow);
DataTemplate dtOpGrRow = cpOpGrRow.ContentTemplate;
DataGrid s2 = (DataGrid)dtOpGrRow.FindName("sumOperationGrid", cpOpGrRow);
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
因此
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentitem((int)_con.Id, new AsyncCallback (GetCurrencySumByCurrentItemCallBack), s2);
}
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
DataGrid s2 = result.AsyncState as DataGrid
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentitem(result).ToList<CurrencySum>();
if (sum != null)
{
s2.Dispatcher.BeginInvoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[2] {s2, sum});
}
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
在这两种情况下,都归结为错误"调用线程无法访问此对象,因为此对象的所有者是另一个线程。"
如何从客户端删除委托的问题。是否在单独的函数中开始GetCurrencySumByCurrentItem并转发数据表s2?如何回到您的流中并将数据加载到表sumOperationGrid中?
如果我理解混乱的代码,我就不明白你为什么要到处通过数据网格。仅尝试在操作结束时通过传递sum来访问网格并设置ItemsSource。以下是回调函数的伪代码:
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
if(sum != null)
{
this.Invoke(new Action(()=>
{
//get data grid
//set ItemSource
})
}
}
或者,如果您确实需要调用数据网格,请尝试这样做:
DataGrid myDataGrid;
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
myDataGrid = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
//do stuff without passing grid
}
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentitem(result).ToList<CurrencySum>();
if (sum != null)
{
myDataGrid.Dispatcher.BeginInvoke(new Action(()=>
{
myDataGrid.ItemSource = sum;
});
}
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
if(sum != null)
{
this.Invoke(new Action(()=>
{
//get data grid
//set ItemSource
})
}
}
我更改了我的代码
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
client.BeginGetCurrencySumByCurrentItem((int)_con.Id, new AsyncCallback(GetCurrencySumByCurrentItemCallBack), null);
}
private delegate void CurrencySumByCurrentItemInvoke(List<CurrencySum> sum);
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentItem(result).ToList<CurrencySum>();
if (sum != null)
{
Dispatcher.Invoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[1] { sum });
}
}
void GetCurrencySumByCurrentItemInvoke(List<CurrencySum> sum)
{
DataGridRow opGrRow = (DataGridRow)(this.operationGrid.ItemContainerGenerator.ContainerFromItem(this.operationGrid.SelectedItem));
DataGridDetailsPresenter cpOpGrRow = FindVisualChild<DataGridDetailsPresenter>(opGrRow);
DataTemplate dtOpGrRow = cpOpGrRow.ContentTemplate;
DataGrid s2 = (DataGrid)dtOpGrRow.FindName("sumOperationGrid", cpOpGrRow);
s2.ItemsSource = sum;
}