【发布时间】:2016-09-06 17:41:49
【问题描述】:
我有一个 WPF 程序,它允许用户在给定零件编号的情况下编辑 SQL 数据表以获取零件详细信息。用户输入零件号,我使用表格适配器将零件号详细信息显示到使用行过滤器的数据网格中。我希望能够获取 ObservableCollection 列表并将其绑定到我的数据网格中的某个列(即,部件类型列有一个部件类型的选择列表下拉列表)。这是我的 .cs:
public MainWindow()
{
createDropDowns();
context = new RefreshAppContext();
InitializeComponent();
PartTypeComboBox.ItemsSource = partTypesList;
#if DEBUG
#endif
}
public void findButton_Click(object sender, RoutedEventArgs e)
{
var partNumber = inputBox.Text;
// Searches for part number in db.
var foundPart = context.RefreshPartTypes.Where(x => x.PartNumber == partNumber).ToList();
if (!foundPart.Any() == true)
{
MessageBox.Show("Part not found. Please try another number.");
}
else
{
// Adapter used to fill DT with info from DB based on query(ies).
adapter_PT.Fill(table_PT);
DataView dv = table_PT.DefaultView;
dv.RowFilter = "PartNumber='" + partNumber + "'";
// Fills data grid in UI.
dg.DataContext = dv;
PartTypeComboBox.ItemsSource = table_LV.DefaultView;
}
}
public void createDropDowns()
{
// Populate part styles from lookup table.
partTypesList = new ObservableCollection<string>();
adapter_LV.Fill(table_LV);
List<string> temp = table_LV.AsEnumerable().Select(x => x[2].ToString()).ToList();
foreach (var partType in temp)
{
partTypesList.Add(partType);
}
partTypesList.Add(""); // Add a blank option in case user needs to input blank cell.
}
这是我的 .xaml:
<DataGrid x:Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="28,-145,-779,0" VerticalAlignment="Top" Height="165" Width="1222" AlternatingRowBackground="LightGray" AlternationCount="2">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="PartTypeComboBox"
Header="PartTypeTest"
DisplayMemberPath="Key"
SelectedValuePath="Id"
SelectedValueBinding="{Binding PartType}"/>
</DataGrid.Columns>
</DataGrid>
我可以让它使用正确的信息列表创建一个新的 ComboBox 列,但我无法让该组合框显示(或绑定?)到从我的 table_PT 数据表填充的 PartType 列上。方向?谢谢!
【问题讨论】: