WPF DataGrid:缺少空行
我创建了一个WPF窗口,使用<代码> DATAGRID,我想在网格底部显示空白的“新项目”行,允许我向网格中添加一个新的项目。由于某些原因,在我的窗口上的网格上没有显示空白行。下面是我用来创建WPF DataGrid:缺少空行,wpf,datagrid,wpfdatagrid,Wpf,Datagrid,Wpfdatagrid,我创建了一个WPF窗口,使用 DATAGRID,我想在网格底部显示空白的“新项目”行,允许我向网格中添加一个新的项目。由于某些原因,在我的窗口上的网格上没有显示空白行。下面是我用来创建DataGrid的标记: <toolkit:DataGrid x:Name="ProjectTasksDataGrid" DockPanel.Dock="Top" Style="{DynamicResource {x:Stati
DataGrid
的标记:
<toolkit:DataGrid x:Name="ProjectTasksDataGrid"
DockPanel.Dock="Top"
Style="{DynamicResource {x:Static res:SharedResources.FsBlueGridKey}}"
AutoGenerateColumns="False"
ItemsSource="{Binding SelectedProject.Tasks}"
RowHeaderWidth="0"
MouseMove="OnStartDrag"
DragEnter="OnCheckDropTarget"
DragOver="OnCheckDropTarget"
DragLeave="OnCheckDropTarget"
Drop="OnDrop"
InitializingNewItem="ProjectTasksDataGrid_InitializingNewItem">
<toolkit:DataGrid.Columns>
<toolkit:DataGridCheckBoxColumn HeaderTemplate="{DynamicResource {x:Static res:SharedResources.CheckmarkHeaderKey}}" Width="25" Binding="{Binding Completed}" IsReadOnly="false"/>
<toolkit:DataGridTextColumn Header="Days" Width="75" Binding="{Binding NumDays}" IsReadOnly="false"/>
<toolkit:DataGridTextColumn Header="Due Date" Width="75" Binding="{Binding DueDate, Converter={StaticResource standardDateConverter}}" IsReadOnly="false"/>
<toolkit:DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" IsReadOnly="false"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
我弄不明白为什么空白行不显示。我尝试了一些显而易见的东西(IsReadOnly=“false”
,CanUserAddRows=“True”
),但运气不佳。你知道为什么空白行被禁用了吗?谢谢你的帮助。文森特·西巴尔发布了一篇文章。有很多种可能性,其中大部分取决于您用于SelectedProject.Tasks
的集合类型
我建议确保“任务”不是只读集合,并且它支持一个必需的接口(在前面的链接中提到),以允许使用DataGrid正确添加新项目。Vincent Sibal发布了一个。有很多种可能性,其中大部分取决于您用于SelectedProject.Tasks
的集合类型
我建议确保“任务”不是只读集合,并且它支持一个必需的接口(在前面的链接中提到),以允许使用DataGrid正确添加新项目。最后回到了这个。我不打算更改已接受的答案(绿色复选标记),但问题的原因如下: 我的视图模型包装了域类,以提供WPF所需的基础结构。我在使用的wrap方法上编写了一个示例,其中包括一个具有两个类型参数的集合类:
VmCollection<VM, DM>
VmCollection
其中DM是一个包装的域类,DM是包装它的WPF类
事实证明,出于某种奇怪的原因,在collection类中使用第二个类型参数会导致WPF DataGrid变得不可编辑。解决方法是消除第二个类型参数
我说不出这为什么有效,只是它确实有效。希望它能帮助其他人。终于回到了这个问题上。我不打算更改已接受的答案(绿色复选标记),但问题的原因如下: 我的视图模型包装了域类,以提供WPF所需的基础结构。我在使用的wrap方法上编写了一个示例,其中包括一个具有两个类型参数的集合类:
VmCollection<VM, DM>
VmCollection
其中DM是一个包装的域类,DM是包装它的WPF类
事实证明,出于某种奇怪的原因,在collection类中使用第二个类型参数会导致WPF DataGrid变得不可编辑。解决方法是消除第二个类型参数
我说不出这为什么有效,只是它确实有效。希望它能帮助其他人。您还必须在集合中的类型上有一个默认构造函数。您还必须在集合中的类型上有一个默认构造函数。向ItemsSource添加一个空项,然后将其删除。执行此操作后,可能必须将CanUserAddRows设置回true。我读了这个解决方案:(Jarrey和Rick Roen的帖子)
当我将ItemsSource设置为DataTable的DefaultView并且该视图为空时,出现了这个问题。不过,这些列是定义的,所以它应该能够获取它们。嘿。在ItemsSource中添加一个空项目,然后将其删除。执行此操作后,可能必须将CanUserAddRows设置回true。我读了这个解决方案:(Jarrey和Rick Roen的帖子)
当我将ItemsSource设置为DataTable的DefaultView并且该视图为空时,出现了这个问题。不过,这些列是定义的,所以它应该能够获取它们。嘿。在我看来,这是DataGrid中的一个bug。帮助我最终认识到了问题所在:DataGrid只有在具有真正的对象绑定时才能识别行的类型。编辑行不显示b/c数据网格不知道列类型。您可能认为绑定强类型集合是可行的,但事实并非如此 要扩展Mike Blandford的答案,必须首先分配空集合,然后添加和删除一行。比如说,
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// data binding
dataGridUsers.ItemsSource = GetMembershipUsers();
EntRefUserDataSet.EntRefUserDataTable dt = (EntRefUserDataSet.EntRefUserDataTable)dataGridUsers.ItemsSource;
// hack to force edit row to appear for empty collections
if (dt.Rows.Count == 0)
{
dt.AddEntRefUserRow("", "", false, false);
dt.Rows[0].Delete();
}
}
在我看来,这是DataGrid中的一个bug。帮助我最终认识到了问题所在:DataGrid只有在具有真正的对象绑定时才能识别行的类型。编辑行不显示b/c数据网格不知道列类型。您可能认为绑定强类型集合是可行的,但事实并非如此 要扩展Mike Blandford的答案,必须首先分配空集合,然后添加和删除一行。比如说,
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// data binding
dataGridUsers.ItemsSource = GetMembershipUsers();
EntRefUserDataSet.EntRefUserDataTable dt = (EntRefUserDataSet.EntRefUserDataTable)dataGridUsers.ItemsSource;
// hack to force edit row to appear for empty collections
if (dt.Rows.Count == 0)
{
dt.AddEntRefUserRow("", "", false, false);
dt.Rows[0].Delete();
}
}
对我来说,实现可编辑异步
DataGrid
的最佳方法如下:
查看模型:
public class UserTextMainViewModel : ViewModelBase
{
private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set
{
this._isBusy = value;
OnPropertyChanged();
}
}
private bool _isSearchActive;
private bool _isLoading;
private string _searchInput;
public string SearchInput
{
get { return _searchInput; }
set
{
_searchInput = value;
OnPropertyChanged();
_isSearchActive = !string.IsNullOrEmpty(value);
ApplySearch();
}
}
private ListCollectionView _translationsView;
public ListCollectionView TranslationsView
{
get
{
if (_translationsView == null)
{
OnRefreshRequired();
}
return _translationsView;
}
set
{
_translationsView = value;
OnPropertyChanged();
}
}
private void ApplySearch()
{
var view = TranslationsView;
if (view == null) return;
if (!_isSearchActive)
{
view.Filter = null;
}
else if (view.Filter == null)
{
view.Filter = FilterUserText;
}
else
{
view.Refresh();
}
}
private bool FilterUserText(object o)
{
if (!_isSearchActive) return true;
var item = (UserTextViewModel)o;
return item.Key.Contains(_searchInput, StringComparison.InvariantCultureIgnoreCase) ||
item.Value.Contains(_searchInput, StringComparison.InvariantCultureIgnoreCase);
}
private ICommand _clearSearchCommand;
public ICommand ClearSearchCommand
{
get
{
return _clearSearchCommand ??
(_clearSearchCommand =
new DelegateCommand((param) =>
{
this.SearchInput = string.Empty;
}, (p) => !string.IsNullOrEmpty(this.SearchInput)));
}
}
private async void OnRefreshRequired()
{
if (_isLoading) return;
_isLoading = true;
IsBusy = true;
try
{
var result = await LoadDefinitions();
TranslationsView = new ListCollectionView(result);
}
catch (Exception ex)
{
//ex.HandleError();//TODO: Needs to create properly error handling
}
_isLoading = false;
IsBusy = false;
}
private async Task<IList> LoadDefinitions()
{
var translatioViewModels = await Task.Run(() => TranslationRepository.Instance.AllTranslationsCache
.Select(model => new UserTextViewModel(model)).ToList());
return translatioViewModels;
}
}
public类UserTextMainViewModel:ViewModelBase
{
私人厕所很忙;
公共图书馆很忙
{
获取{return\u isBusy;}
设置
{
这个。_isBusy=value;
OnPropertyChanged();
}
}
私人学校是研究性的;
私家车装载;
私有字符串_searchInput;
公共字符串搜索输入
{
获取{return\u searchInput;}
设置
{
_搜索输入=值;
OnPropertyChanged();
_isSearchActive=!string.IsNullOrEmpty(值);
ApplySearch();
}
}
private ListCollectionView _TranslationView;
公共列表集合视图转换视图
{
得到
{
如果(_translationView==null)
{
OnRefreshRequired();
}
返回翻译视图;
}
设置
{
_translationsView=值;
OnPropertyChanged();
}
}
私有void ApplySearch()
{
变量视图=转换vi