ListView中的绑定问题

时间:2015-11-02 06:29:12

标签: c# wpf xaml

我遇到了为ListView Item绑定w.r.t的问题。问题是:

  

System.Windows.Data错误:4:找不到绑定源   参考' RelativeSource FindAncestor,   AncestorType =' System.Windows.Controls.ItemsControl&#39 ;,   AncestorLevel =' 1'&#39 ;. BindingExpression:路径= VerticalContentAlignment;   的DataItem = NULL;目标元素是' ListViewItem' (名称='&#39);目标   属性是< VerticalContentAlignment' (键入' VerticalAlignment')

     

System.Windows.Data错误:4:找不到绑定源   参考' RelativeSource FindAncestor,   AncestorType =' System.Windows.Controls.ItemsControl&#39 ;,   AncestorLevel =' 1'&#39 ;. BindingExpression:路径= Horizo​​ntalContentAlignment;   的DataItem = NULL;目标元素是' ListViewItem' (名称='&#39);目标   属性是' Horizo​​ntalContentAlignment' (键入' Horizo​​ntalAlignment')

我有一个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>

1 个答案:

答案 0 :(得分: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。所以我应该将StackPanel键入AncestorType

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