我在保留DataGridTemplate列组合框中的选定项目时遇到问题。 我有DataTemplate可编辑组合框列作为数据网格中的第一列,并在它旁边,我有一个文本列。 DataGrid中填充了从SQL存储过程中读取的数据。一切正常,除了当我选择组合框中的项目并移动到文本字段并开始输入时,组合选择将变为空白。它为新项目或现有项目填补了空白。奇怪的是,这只是第一次发生。当我重新选择组合框值或再次添加新项目并返回到文本字段时,它不会空白。我正在用尽想法,尝试了很多组合,但目前为止还没有运气。 这里是我的代码:DataGrid组合框与所选项目或新项目的绑定问题
这是怎么了填充DataGrid:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "GetProducts";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConn;
var reader = cmd.ExecuteReader();
var dt = new DataTable();
dt.Load(reader);
dt.Columns["ProductName"].AllowDBNull = true;
dtProductCfgTable = dt;
ProductCfgGrid.ItemsSource = dtProductCfgTable.DefaultView;
}
这是ProductNamesList声明:
public List<string> ProductNamesList { get; set; }
XAML:
<DataGridTemplateColumn Header="ProductName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ProductNamesList,
RelativeSource={RelativeSource AncestorType=Window}}"
SelectedItem="{Binding ProductName
IsSynchronizedWithCurrentItem="False"
BorderThickness="1.2 1.2 0 0" BorderBrush="Black"
Background="LightCyan" IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding ShippingAddress}"
Width="100"
Header="ShippingAddress"
Visibility="Visible"/>
你有什么设置你的datacontext? – Shoe
我没有设定在这种情况下,电网的任何的datacontext –
看看http://stackoverflow.com/questions/3743269/editable-combobox-with-binding-to-value-not-in-list和尝试那些解决方案 – Shoe