1

我在绑定ListBox.ItemSource到窗口包含ObservableCollection的对象内的对象时遇到问题。DataContext

主窗口.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;

数据上下文对象.cs

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

MyObj包含名为 的 ObservableCollection MyCollection。它没有约束力:(当我试图把一切都完美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>
4

0 回答 0