我对 WPF 和 .NET 有点陌生,所以这可能是一项简单的任务:
我有文本块,它们从数据网格的选定项中获取它们的值。我正在尝试将组合框绑定到该数据网格中的一列,以便用户看到该数据网格列中的所有值。在组合框中选择一个项目时,它应该使该行也成为数据网格中的选定项目。
这是我的数据网格:
<sdk:DataGrid AutoGenerateColumns="True" Name="l1dGrid" IsReadOnly="True" ItemsSource="{Binding}" DataContext="{Binding Path=DataContext}" />
这是 Datagrid 加载数据的地方:
_PCContext.Load(_PCContext.GetLine1_DownstairsQuery());
l1dGrid.ItemsSource = _PCContext.Line1_Downstairs;
现在我只需要一个组合框就可以更改 DataGrid 中的选定项目。
提前感谢您的帮助!!!
[编辑 - 已解决]
好的,所以我最终只是根据组合框的选定项查询数据网格,并将数据网格选定项设置为与该查询匹配的项。
这是我用来执行此操作的代码:
private void stockPick_comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selection = stockPick_comboBox.SelectedValue.ToString().Replace("Line1_Downstairs : ", "");
selectGridItem(selection);
}
private void selectGridItem(string selection)
{
var stock = (from i in _PCContext.Line1_Downstairs
where i.Stock == selection
select i).FirstOrDefault();
l1dGrid.SelectedItem = stock;
}