我有一个列表框、数据网格和一个按钮。数据网格中填充了来自 MS SQL 的数据。我希望能够使用按钮将所选项目从数据网格复制到列表框。按钮后面的代码是
private void btnAdd_click(object sender, RoutedEventArgs e)
{
lstSelected.Items.Add(iFacilitiesDataGrid.SelectedItem.ToString());
}
//List Box in xaml
<ListBox Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="258" HorizontalAlignment="Left" Margin="0,4,0,0" Name="lstSelected" VerticalAlignment="Top" Width="236" />
对于数据网格
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="244" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource iLocationICategoriesIFacilitiesViewSource}}" Margin="291,5,0,0" Name="iFacilitiesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="247">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="facilityNameColumn" Header="Facility Name" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FacilityName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="priceColumn" Binding="{Binding Path=Price}" Header="Price" Width="100" />
</DataGrid.Columns>
</DataGrid>
每当我尝试在列表框上添加项目时,传递给列表框的项目是“HM.IFacility”,其中 HM 是项目名称,IFacility 是表名称。