异常:输入字符串的格式不正确
我不确定我如何得到这个。仅当我已经尝试放置值时尝试重新输入单元格时,才会发生这种情况。因此,如果用户想要编辑他们先前输入的内容,则会产生此异常。
我有这个替代,因此我可以利用IsEditable属性。
在运行函数GenerateEditingElement之后产生异常。如果我跳过调试代码,我会得到这个。
您的应用已进入中断状态,但由于所有线程都在执行外部代码(通常是系统代码或框架代码),因此没有可显示的代码。
public class DataGridComboBoxColumn : DataGridBoundColumn {
public Binding ItemsSourceBinding { get; set; }
public string DisplayMemberPath { get; set; }
public string SelectedValuePath { get; set; }
public object ItemSource { get; set; }
public int SelectedIndex { get; set; } = -1;
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) {
var textBox = new TextBlock();
if (Binding != null) {
BindingOperations.SetBinding(textBox, TextBlock.TextProperty, Binding);
}
return textBox;
}
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) {
var comboBox = new ComboBox { IsEditable = true };
if (Binding != null && ItemsSourceBinding != null) {
comboBox.DisplayMemberPath = DisplayMemberPath;
comboBox.SelectedValuePath = SelectedValuePath;
BindingOperations.SetBinding(comboBox, ComboBox.SelectedValueProperty, Binding);
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, ItemsSourceBinding);
}
return comboBox;
}
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) {
var comboBox = editingElement as ComboBox;
if (comboBox == null) return null;
if (SelectedIndex != -1 ) { comboBox.SelectedIndex = SelectedIndex; } else { comboBox.SelectedIndex = -1; }
comboBox.Focus(); // This solves the double-tabbing problem that Nick mentioned.
return comboBox.Text;
}
protected override bool CommitCellEdit(FrameworkElement editingElement) {
return base.CommitCellEdit(editingElement);
}
protected override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue) {
base.CancelCellEdit(editingElement, uneditedValue);
}
}
XAML
<local:DataGridComboBoxColumn Header="Prod #" x:Name="cboProd" Width="60" Binding="{Binding ProductNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="ProductNum" SelectedValuePath="ProductsID" ItemsSourceBinding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:PurMaterialEntry}}, Path=PDN}"/>
PDN是我在该项目旁边的组合框中收集的该项目的产品编号。
我在寻找什么。
-我希望它在重新输入时清除单元格。
-网格中的项目ComboBox控制了该项目。
Image showing that the combobox works during entry.
但是,如果我必须修改产品D101,则会发生异常。