可绑定属性不在用户控件wpf中工作

时间:2015-03-27 10:27:28

标签: c# wpf

我有用户控制并在该用户控件中创建DepedencyProperty“ItemSource”,如下所示

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;
     }
 }

然后我像这样将“ItemSource”绑定到UserControl里面的DataGrid的ItemSource中。

<UserControl x:Class="WPF.Controls.DataGridFilterDetail" x:Name="dataGridFilterDetail" .../>
    <DataGrid ItemsSource="{Binding ElementName=dataGridFilterDetail,Path=ItemsSource}" AutoGenerateColumns="True"/>
</UserControl>

然后我在我的视图中使用此用户控件。

<UserControl x:Class="Project.View.MyView" ..
<UserControl.Resources>
        <vm:MyViewModel x:Key="myViewModel" />
</UserControl.Resources>
<Grid>
    <custom:DataGridFilterDetail ItemsSource="{Binding GridItemCollections, Source={StaticResource myViewModel}}" />
</Grid>
</UserControl>

在MyView.cs构造函数中我设置了我的视图datacontext:

public MyView()
        {
            InitializeComponent();
            myViewModel= (MyViewModel)this.Resources["myViewModel"];
            this.DataContext = myViewModel;
        }
在myViewModel上的

我创建属性“GridItemCollections”,就像这样

 private ObservableCollection<myCustomClass> gridItemCollections;
 public ObservableCollection<myCustomClass> GridItemCollections
 {
     get { return gridItemCollections; }
     set
     {
         gridItemCollections = value;
         NotifyPropertyChanged("GridItemCollections");
     }
 }

 public void AddNewGridRow()
 {
    GridItemCollections.Add(CurrentDetail);
 }

现在的问题是,当我在我的视图模型上修改“GridItemCollections”时,为什么用户控件中的网格不会更新?我在这里缺少什么? 提前谢谢。

0 个答案:

没有答案