【发布时间】:2018-03-10 16:57:45
【问题描述】:
我正在尝试基于 WPF 的 DataGrid 控件创建自定义 DataGrid。我创建了另一个名为“ItemsDataSource”的属性,并使用它来绑定来自我的 ViewModel 的集合。当此属性引发 ValueChanged 事件时,它将 ItemsSource 的值设置为 ItemsDataSource 的值。
当网格处于只读模式时,这可以正常工作,但是当我将属性 CanUserAddRows 设置为 True 时,如果 ItemsDataSource 为空,我的 DataGrid 永远不会显示新行来添加新行。但是,如果我将绑定更改回 ItemsSource 而不是我的 ItemsDataSource,DataGrid 会显示新行。
这是我的自定义网格的部分代码:
public partial class NewDataGrid : DataGrid
{
public NewDataGrid()
{
InitializeComponent();
var dpd = DependencyPropertyDescriptor.FromProperty(ItemsDataSourceProperty, typeof(NewDataGrid));
dpd?.AddValueChanged(this, (s, a) =>
{
ItemsSource = ItemsDataSource.Cast<object>().ToList();
});
}
public IList ItemsDataSource
{
get { return (IList)GetValue(ItemsDataSourceProperty); }
set { SetValue(ItemsDataSourceProperty, value); }
}
public static readonly DependencyProperty ItemsDataSourceProperty =
DependencyProperty.Register("ItemsDataSource", typeof(IList), typeof(NewDataGrid), new PropertyMetadata(null));
}
这是我在 XAML 中进行绑定的方式:
<WPF:NewDataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
ItemsDataSource="{Binding Path=DataContext.DataWrapperList, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
SelectedValue="{Binding Path=DataContext.SelectedDataWrapper, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
AutoGenerateColumns="False"
Validation.ErrorTemplate="{x:Null}"
Margin="0"
VerticalScrollBarVisibility="Auto"
SelectionMode="Single"
CanUserAddRows="True"
CanUserDeleteRows="True"
IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*" SortMemberPath="Name" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Path=Quantity}" Width="*" SortMemberPath="Quantity" />
</WPF:PAENewDataGrid>
这是在我的 DataContext 中声明 DataListWrapper 属性的方式:
public ObservableCollection<DataWrapper> DataWrapperList;
这是我的 DataWrapper 类:
public class DataWrapper : BaseWrapper
{
private DataWrapperDTO _data;
public DataWrapper()
{
_data = new DataWrapperDTO();
}
public DataWrapper(DataWrapperDTO data)
{
_data = data;
}
public string Name
{
get { return _data.Name; }
set
{
_data.Name = value;
RaisePropertyChanged(nameof(Name));
}
}
public int Quantity
{
get { return _data.Quantity; }
set
{
_data.Quantity = value;
RaisePropertyChanged(nameof(Quantity));
}
}
}
有谁知道当 CanUserAddRows 属性设置为 True 时如何强制 DataGrid 始终显示这一新行?
【问题讨论】:
-
您能否提供
DataWrapperList属性。我要检查的另一件事是删除ItemsSource = ItemsDataSource.Cast<object>().ToList();中的Cast<object>().ToList()。我目前不在电脑前用 vs 来检查它,但我猜ItemsSource只是一个List<object>,列表中有一个项目。 -
@MartinBackasch 我已经按照您的建议进行了更改,但仍然无法正常工作:(
-
直接绑定到 ItemsSource 有什么问题?
-
与您的问题文本相反,
DataListWrapper property不是属性,而是字段。它可能需要成为一个属性。
标签: c# wpf mvvm wpfdatagrid