当用户点击最后一行添加新行时,标准WPF DataGrid
中存在一个已知错误。
抛出异常是因为在处理代表'NewItemPlaceholder'的MS.Internal.NamedObject
时,ConvertBack方法(在默认转换器上)失败。如果CanUserAddRows设置为True(并且集合支持它),则此实例用于表示空白的“新行”。实际上,在尝试跟踪绑定失败时,似乎实际上在异常处理程序中抛出了FormatException。有关详细信息,请参阅Nigel Spencer's Blog。
基本上解决方法是在SelectedItem
绑定上添加转换器:
public class IgnoreNewItemPlaceholderConverter : IValueConverter
{
private const string newItemPlaceholderName = "{NewItemPlaceholder}";
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value.ToString() == newItemPlaceholderName)
value = DependencyProperty.UnsetValue;
return value;
}
}
在XAML中使用它的一个例子是:
<Window.Resources>
<converters:IgnoreNewItemPlaceHolderConverter x:Key="ignoreNewItemPlaceHolderConverter"/>
</Window.Resources>
<toolkit:DataGrid ItemsSource="{Binding Persons}"
AutoGenerateColumns="False"
SelectedItem="{Binding SelectedPerson, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}"
IsSynchronizedWithCurrentItem="True">...</toolkit:DataGrid>
我的问题是我试图将此“修复”/“黑客”实施到我自己的DataGrid
但没有成功。我有一个自定义DataGrid
,其中我通过以下方式覆盖了标准DataGrid
控件:
/// <summary>
/// Class that overrides the standard DataGrid and facilitates the
/// the loading and binding of multiple cultures.
/// </summary>
public class ResourceDataGrid : DataGrid
{
private IResourceStrategy strategy;
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == DataContextProperty)
HandleDataContextChanged(e.OldValue, e.NewValue);
if (e.Property == ItemsSourceProperty)
HandleItemsSourceChanged(e.OldValue, e.NewValue);
}
private void HandleDataContextChanged(object oldValue, object newValue)
{
if (strategy != null)
strategy.ResourceCulturesChanged -= Strategy_ResourceAdded;
// Pull in the required data from the strategy.
var resourceDataViewModel = newValue as ResourceDataViewModel;
if (resourceDataViewModel == null)
return;
strategy = resourceDataViewModel.Strategy;
strategy.ResourceCulturesChanged += Strategy_ResourceAdded;
}
private void Strategy_ResourceAdded(object sender, ResourceCollectionChangedEventArgs args)
{
UpdateGrid();
}
private void HandleItemsSourceChanged(object oldValue, object newValue)
{
if (Equals(newValue, oldValue))
return;
UpdateGrid();
}
private void UpdateGrid()
{
if (strategy == null) return;
// Update the bound data set.
foreach (CollectionTextColumn item in Columns.OfType<CollectionTextColumn>().ToList())
{
// Remove dynamic columns of the current CollectionTextColumn.
foreach (var dynamicColumn in Columns.OfType<DynamicTextColumn>().ToList())
Columns.Remove(dynamicColumn);
int itemColumnIndex = Columns.IndexOf(item) + 1;
string collectionName = item.Collection;
List<string> headers = strategy.ResourceData.FileCultureDictionary.Select(c => c.Value).ToList();
// Check if ItemsSource is IEnumerable<object>.
var data = ItemsSource as IEnumerable<object>;
if (data == null)
return;
// Copy to list to allow for multiple iterations.
List<object> dataList = data.ToList();
var collections = dataList.Select(d => GetCollection(collectionName, d));
int maxItems = collections.Max(c => c.Count());
for (int i = 0; i < maxItems; i++)
{
// Header binding.
string header = GetHeader(headers, i);
Binding columnBinding = new Binding(String.Format("{0}[{1}]", item.Collection, i));
Columns.Insert(itemColumnIndex + i,
new DynamicTextColumn(item) { Binding = columnBinding, Header = header });
}
}
}
private IEnumerable<object> GetCollection(string collectionName, object collectionHolder)
{
// Reflect the property which holds the collection.
PropertyInfo propertyInfo = collectionHolder.GetType().GetProperty(collectionName);
object propertyValue = propertyInfo.GetValue(collectionHolder, null);
var collection = propertyValue as IEnumerable<object>;
return collection;
}
private static string GetHeader(IList<string> headerList, int index)
{
int listIndex = index % headerList.Count;
return headerList[listIndex];
}
}
为了显示绑定,我在XAML中使用ResourceDataGrid
,如下所示:
<Window.Resources>
<converters:IgnoreNewItemPlaceHolderConverter x:Key="ignoreNewItemPlaceHolderConverter"/>
</Window.Resources>
<Controls:ResourceDataGrid x:Name="resourceDataGrid"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedResource, Converter={StaticResource ignoreNewItemPlaceholderConverter}, Mode=TwoWay}"
ItemsSource="{Binding Path=Resources, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}">
<Controls:ResourceDataGrid.Columns>
<DataGridTemplateColumn Header="KeyIndex" SortMemberPath="KeyIndex" CellStyle="{StaticResource MetroDataGridCell}"
CellTemplate="{StaticResource readOnlyCellUpdatedStyle}" IsReadOnly="True"/>
<DataGridTextColumn Header="FileName" CellStyle="{StaticResource MetroDataGridCell}"
Binding="{Binding FileName}" IsReadOnly="True"/>
<DataGridTextColumn Header="ResourceName" Binding="{Binding ResourceName}"
CellStyle="{StaticResource MetroDataGridCell}" IsReadOnly="False"/>
<Controls:CollectionTextColumn Collection="ResourceStringList" Visibility="Collapsed"
CellStyle="{StaticResource MetroDataGridCell}"/>
</Controls:ResourceDataGrid.Columns>
</Controls:ResourceDataGrid>
现在,我实现IgnoreNewItemPlaceHolderConverter
转换器并调用它并设置DependencyProperty.UnsetValue
;这是它的工作。但是,已调用被覆盖的OnPropertyChanged
事件且DependencyPropertyChangedEventArgs
e
包含验证错误:
ErrorContent =“无法转换”值'{NewItemPlaceholder}'。“
我已经实现了一个包含两列的基本示例,这是有效的。 这是由于我的更复杂的自定义DataGrid
,如何阻止此验证错误发生?
感谢您的时间。
答案 0 :(得分:5)
如果你做了mvvm,你可以采取另一种方式,如果你可行的话。 我在我的项目中设置了CanUserAddRows =“False”,但是添加了一个带有ICommand“AddNewItemCommand”的按钮 在这些命令中,我只需在我的ItemsSource集合中添加一个新项目 - 然后我就完成了:)
如果这对你没办法 - 只要忽略我的答案:)
答案 1 :(得分:1)
我设法通过从拖动列表中删除元素来获得解决方法,请参阅下面的代码段:
private void preventDragEmpty(object sender, DragEventArgs e)
{
List<dynamic> h = new List<dynamic>();
try
{
//i'm using GongSolutions to handle drag and drop wich is highlly recommended
//but if you dont use it just adapt to the correct type!
h = e.Data.GetData("GongSolutions.Wpf.DragDrop") as List<dynamic>;
if (h != null)
{
h.Remove(h.FirstOrDefault(x => x.ToString() == "{NewItemPlaceholder}"));
e.Data.SetData(h);
}
}
finally
{
e.Handled = true;
}
}
要使用它,您可以附加到任何类型的列表,如:
<DataGrid ... PreviewDragOver="preventDragEmpty" />