C# 可绑定属性在用户控件wpf中不起作用
我有用户控件,并在该用户控件内创建DependencProperty ItemSource,如下所示C# 可绑定属性在用户控件wpf中不起作用,c#,wpf,C#,Wpf,我有用户控件,并在该用户控件内创建DependencProperty ItemSource,如下所示 public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( "ItemsSource", typeof(IEnumerable), typeof(DataGridFilterDetail), new PropertyMetadat
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource", typeof(IEnumerable), typeof(DataGridFilterDetail),
new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
NotifyPropertyChanged("ItemsSource");
}
}
static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as DataGridFilterDetail;
if (control != null)
{
control.OnItemsSourceChanged((IEnumerable)e.OldValue(IEnumerable)e.NewValue);
}
}
void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
// Remove handler for oldValue.CollectionChanged
var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (null != oldValueINotifyCollectionChanged)
{
oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
// Add handler for newValue.CollectionChanged (if possible)
var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (null != newValueINotifyCollectionChanged)
{
newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
}
void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems == null)
{
TotalCount = 0;
}
else
{
TotalCount = e.NewItems.Count;
}
}
private ObservableCollection<myCustomClass> gridItemCollections;
public ObservableCollection<myCustomClass> GridItemCollections
{
get { return gridItemCollections; }
set
{
gridItemCollections = value;
NotifyPropertyChanged("GridItemCollections");
}
}
public void AddNewGridRow()
{
GridItemCollections.Add(CurrentDetail);
}
然后我将ItemSource绑定到UserControl内的DataGrid的ItemSource中,如下所示
<UserControl x:Class="WPF.Controls.DataGridFilterDetail" x:Name="dataGridFilterDetail" .../>
<DataGrid ItemsSource="{Binding ElementName=dataGridFilterDetail,Path=ItemsSource}" AutoGenerateColumns="True"/>
</UserControl>
在myViewModel上,我创建了如下属性GridItemCollections
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource", typeof(IEnumerable), typeof(DataGridFilterDetail),
new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
NotifyPropertyChanged("ItemsSource");
}
}
static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as DataGridFilterDetail;
if (control != null)
{
control.OnItemsSourceChanged((IEnumerable)e.OldValue(IEnumerable)e.NewValue);
}
}
void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
// Remove handler for oldValue.CollectionChanged
var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (null != oldValueINotifyCollectionChanged)
{
oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
// Add handler for newValue.CollectionChanged (if possible)
var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (null != newValueINotifyCollectionChanged)
{
newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
}
void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems == null)
{
TotalCount = 0;
}
else
{
TotalCount = e.NewItems.Count;
}
}
private ObservableCollection<myCustomClass> gridItemCollections;
public ObservableCollection<myCustomClass> GridItemCollections
{
get { return gridItemCollections; }
set
{
gridItemCollections = value;
NotifyPropertyChanged("GridItemCollections");
}
}
public void AddNewGridRow()
{
GridItemCollections.Add(CurrentDetail);
}
现在的问题是,当我在视图模型上修改GridItemCollections时,为什么用户控件中的grid没有更新?这里是否缺少一些东西?
提前感谢。您的DataGridFilterDetail控件中的OnItemSourceProperty发生了什么变化?这似乎没有必要。您也不需要在依赖项属性设置程序中引发PropertyChanged事件。UserControl通常不实现INotifyPropertyChanged.OnItemSourcePropertyChanged我为我的用户控件设置新集合如下:var control=sender as DataGridFilterDetail;如果控制!=null{control.onitemsourcechangedienumerablee.OldValue,IEnumerablee.NewValue;}我知道我不需要提升属性更改,我有点绝望:所以我尝试了这些。嗨,克莱门斯,我已经在我的问题中添加了源代码。嗨,克莱门斯,我已经在我的问题中添加了其余的代码。