Nisarga Nagaraj
Nisarga Nagaraj

Reputation: 69

Binding issues in ListView

I am getting issues w.r.t binding for the ListView Item. The issue is:

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')

I have a ListView, and have set the ItemContainerStyle, still I am getting the same issue. Please help

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

Upvotes: 0

Views: 1070

Answers (1)

StepUp
StepUp

Reputation: 38199

I cannot reproduce your error. It works okay. It seems to me that you have some incorrect initializations at properties at view model. See the work example of MVVM ListView .

Model:

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

ViewModel:

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
    } 

View with your features:

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

Pay attention that I've binded DataContext to StackPanel. So I should type that StackPanel as AncestorType:

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

Upvotes: 0

Related Questions