2015-11-02 92 views
0

我得到的問題w.r.t綁定的ListView項目。問題是:在ListView綁定問題

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

我有一個ListView,並已設置ItemContainerStyle,仍然我得到同樣的問題。請幫助

<ListView Width="Auto" Height="1" Name="ListViewDetails" 
      ItemsSource="{Binding DetailsObservableCollection}"> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
+0

你從哪裏得到這個錯誤?這是一個編譯錯誤,還是你在調試輸出窗口中看到它?我無法用你的代碼重現這一點。 – Jens

+0

您的datacontext在哪裏定義? – Tomtom

+0

用'ListView.Resources'替換'ListView.ItemContainerStyle' – Tomtom

回答

0

我無法重現您的錯誤。它工作正常。在我看來,你在視圖模型的屬性處有一些不正確的初始化。查看MVVM ListView的工作示例。

型號:

public class Person 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Author { get; set; } 
    public bool IsClickable { get; set; } 
} 

視圖模型:

public class MainWindowViewModel:INotifyPropertyChanged 
    {   
     private ObservableCollection<Person> _persons=new ObservableCollection<Person>(); 
     public ObservableCollection<Person> Persons 
     { 
      get 
      { 
       return _persons=GetData(); 
      } 
      set 
      { 
       _persons = value; 
       OnPropertyChanged("Persons"); 
      } 
     }   

     public ObservableCollection<Person> GetData() 
     { 
      myDataList.Add(new Person() { ID = 1, Name = "Person1", Author = "Author1", Price = "6.7 TL", Catalog = "IT", IsClickable=true}); 
      myDataList.Add(new Person() { ID = 2, Name = "Person2", Author = "Author2", Price = "9.7 TL", Catalog = "IT", IsClickable = false}); 
      myDataList.Add(new Person() { ID = 3, Name = "Person3", Author = "Author3", Price = "11.7 TL", Catalog = "IT", IsClickable = true}); 
      myDataList.Add(new Person() { ID = 2, Name = "Person4", Author = "Author2", Price = "9.7 TL", Catalog = "IT", IsClickable = true}); 
      myDataList.Add(new Person() { ID = 3, Name = "Person5", Author = "Author3", Price = "11.7 TL", Catalog = "IT", IsClickable = false}); 
      if (myDataList.Count > 0) 
      { 
       return myDataList; 
      } 
      else 
       return null; 
     } 

     RelayCommand _clickCommand = null; 
     public ICommand SomeClickCommand 
     { 
      get 
      { 
       if (_clickCommand == null) 
       { 
        _clickCommand = new RelayCommand((p) => OnClick(p), (p) => CanClick(p)); 
       } 

      return _clickCommand; 
      } 
     } 

     private bool CanClick(object obj) 
     { 
      return true; 
     } 

     private void OnClick(object obj) 
     { 
      MessageBox.Show("You clicked:)"); 
     } 

     #region OnPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 
     public void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs("propertyName")); 
     } 
     #endregion 
    } 

查看你的特點:

<Window x:Class="WpfApplication1.MainWindow" 
     ... 
     xmlns:local="clr-namespace:WpfApplication1.ViewModel" 
     Title="MainWindow" Height="550" Width="525"> 
    <Window.Resources> 
     <local:MainWindowViewModel x:Key="mainWindowViewModel"/> 
    </Window.Resources> 
    <StackPanel DataContext="{StaticResource mainWindowViewModel}"> 
    <ListView ItemsSource="{Binding Persons}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <WrapPanel>       
        <TextBlock Text="{Binding Name}" Background="LightGreen" Margin="1"/> 
        <TextBlock Text="{Binding Author}" Background="LightCyan" Margin="1"/> 
        <TextBlock Text="{Binding TimeGap}" Background="LightCoral" Margin="1"/>       
        <CheckBox IsChecked="{Binding IsClickable}" CommandParameter="{Binding}" Command="{Binding DataContext.SomeClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Margin="1"/> 

       </WrapPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate>    
    </ListView> 
    </StackPanel> 
</Window> 

講究我已將DataContext綁定到StackPanel。所以我應該輸入StackPanelAncestorType

<CheckBox IsChecked="{Binding IsClickable}" CommandParameter="{Binding}" Command="{Binding DataContext.SomeClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Margin="1"/> 
+0

我有一個複選框,並在複選框的check/uncheck命令中,listview項目正在加載。複選框的代碼就像這樣,我在輸出窗口中看到上面提到的問題。所以我無法理解爲什麼會出現綁定問題。 –

+0

@NisargaNagaraj看到我更新的答案。 – StepUp

+0

@NisargaNagaraj你可以接受我的回覆作爲答案,如果它對你有幫助:)。 – StepUp