我正在尝试创建建议列表。这意味着当我键入条形码时,所有以我键入的内容开头的条形码都应显示在另一个数据网格中。 数据网格显示的行数正确,但即使标题未显示,内容也不正确。
我尝试创建具有6列的Datagrid,并编写了代码以在条形码文本框的TextChanged属性中显示Datagrid。
我的XAML部分:
<Grid Grid.Column="0" Grid.Row="1" Margin="10">
<DataGrid CanUserAddRows="False" Visibility="Hidden" Style="{StaticResource DataGridListing}" x:Name="DataGridSalesSuggestion" Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type Grid}, Mode=FindAncestor}}" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type Grid}, Mode=FindAncestor}}">
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding barcode, Source={StaticResource Strings}}" x:Name="DataGridSuggestionItemName" Binding="{Binding BarCode}" />
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding ItemCode, Source={StaticResource Strings}}" x:Name="DataGridSuggestionItemCode" Binding="{Binding ItemCode}" />
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding itemName, Source={StaticResource Strings}}" x:Name="DataGridSuggestionBarcode" Binding="{Binding ItemName}" />
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding UnitQuantity, Source={StaticResource Strings}}" x:Name="DataGridSuggestionUnitQty" Binding="{Binding UnitQuantity}" />
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding price, Source={StaticResource Strings}}" x:Name="DataGridColumnSuggestionPrice" Binding="{Binding PricePerUnit}" />
<DataGridTextColumn IsReadOnly="True" Width="1*" Header="{Binding StockQty, Source={StaticResource Strings}}" x:Name="DataGridColumnSuggestionStockQty" Binding="{Binding StockQuantity}" />
</DataGrid>
</Grid>
隐藏代码:
private void TextboxBarCodeTextchanged(object sender, TextChangedEventArgs e)
{
Stock obj = DataGridSalesDetails.SelectedItem as Stock;
DataGridSalesSuggestion.Items.Clear();
for (int i = 0; i < AllStockList.Count; i++)
{
if (!string.IsNullOrEmpty(obj.BarCode))
{
if (AllStockList[i].BarCode.StartsWith(obj.BarCode, StringComparison.InvariantCultureIgnoreCase))
{
Stock vend = AllStockList[i] as Stock;
DataGridSalesSuggestion.Items.Add(vend);
DataGridSalesSuggestion.Visibility = Visibility.Visible;
}
}
}
if (DataGridSalesSuggestion.Items.Count > 0)
{
DataGridSalesSuggestion.Visibility = Visibility.Visible;
}
if (string.IsNullOrEmpty(obj.BarCode))
{
DataGridSalesSuggestion.Visibility = Visibility.Collapsed;
}
}
预期结果是此Datagrid应该显示与我在条形码文本框中键入内容相关的内容。
实际结果是此代码获取显示一个空的Datagrid。即使标题不显示。有人可以帮忙吗? .net是新手