将ListBox ItemsSource绑定到对象内的ObservableCollection

时间:2013-01-08 17:49:43

标签: c# wpf binding observablecollection

我遇到了ListBox.ItemSourceObservableCollection对象的问题,该对象位于窗口DataContext包含的对象内。

MainWindow.xaml

<ListBox Name="resourcesListBox"
     ItemsSource="{Binding MyObj.MyCollection}"
     Height="366"
     DockPanel.Dock="Top"

     Margin="10,5,10,0"
     VerticalContentAlignment="Top"
     >

     <ListBox.ItemTemplate>
        <DataTemplate>
           <Grid Margin="5">

               <Grid.ColumnDefinitions>
                   <ColumnDefinition Width="50" />
                   <ColumnDefinition Width="*" />
               </Grid.ColumnDefinitions>
               <Grid.RowDefinitions>
                   <RowDefinition Height="40" />
               </Grid.RowDefinitions>
               <Image Source="{Binding Image}"
                      Grid.Column="0"
                      Grid.Row="0"
                      Margin="0,0,5,0" />
               <TextBlock Grid.Row="0"
                          Grid.Column="1"
                          Text="{Binding Name}"
                          Margin="0,10,0,0" />
          </Grid>
       </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

MainWindow.xaml.cs构造函数

dataContextObj= new DataContextObject();
this.DataContext = dataContextObj;

DataContextObject.cs

private MyObj myObj;
    public MyObj MyObj
    {
        get
        {
            return myObj;
        }
        set
        {
            myObj= value;
            OnPropertyChanged("MyObj");
        }
    }

MyObj包含名为MyCollection的ObservableCollection。 它没有约束力:(当我试图将MyCollection放入DataContextObject时,一切都很完美。为什么?

修改

    public class MyObj: INotifyPropertyChanged
    {
        private ObservableCollection<O> myCollection;
        public ObservableCollection<O> MyCollection
        {
            get
            {
                return myCollection;
            }
            set
            {
                myCollection= value;
                OnPropertyChanged("MyCollection");
            }
         }
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
...
}

编辑2 输出

System.Windows.Data Warning: 55 : Created BindingExpression (hash=41973697) for Binding (hash=27212573)
System.Windows.Data Warning: 57 :   Path: 'MyObj.MyCollection'
System.Windows.Data Warning: 59 : BindingExpression (hash=41973697): Default mode resolved to OneWay
System.Windows.Data Warning: 60 : BindingExpression (hash=41973697): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 61 : BindingExpression (hash=41973697): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=36139932)
System.Windows.Data Warning: 66 : BindingExpression (hash=41973697): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=41973697): Found data context element: ListBox (hash=36139932) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=41973697): DataContext is null
System.Windows.Data Warning: 64 : BindingExpression (hash=41973697): Resolve source deferred

...

System.Windows.Data Warning: 66 : BindingExpression (hash=41973697): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=41973697): Found data context element: ListBox (hash=36139932) (OK)
System.Windows.Data Warning: 77 : BindingExpression (hash=41973697): Activate with root item DataContextObject (hash=30369281)
System.Windows.Data Warning: 107 : BindingExpression (hash=41973697):   At level 0 - for DataContextObject.MyObj found accessor RuntimePropertyInfo(ResourcesListViewModel)
System.Windows.Data Warning: 103 : BindingExpression (hash=41973697): Replace item at level 0 with DataContextObject (hash=30369281), using accessor RuntimePropertyInfo(MyObj)
System.Windows.Data Warning: 100 : BindingExpression (hash=41973697): GetValue at level 0 from DataContextObject (hash=30369281) using RuntimePropertyInfo(MyObj): MyObj (hash=60716890)
System.Windows.Data Warning: 107 : BindingExpression (hash=41973697):   At level 1 - for MyObj.MyCollection found accessor RuntimePropertyInfo(MyCollection)
System.Windows.Data Warning: 103 : BindingExpression (hash=41973697): Replace item at level 1 with MyObj(hash=60716890), using accessor RuntimePropertyInfo(MyCollection)
System.Windows.Data Warning: 100 : BindingExpression (hash=41973697): GetValue at level 1 from MyObj(hash=60716890) using RuntimePropertyInfo(MyCollection): <null>
System.Windows.Data Warning: 79 : BindingExpression (hash=41973697): TransferValue - got raw value <null>
System.Windows.Data Warning: 88 : BindingExpression (hash=41973697): TransferValue - using final value <null>

0 个答案:

没有答案