首页 文章

ListView中的绑定问题

提问于
浏览
0

我正在为ListView Item绑定w.r.t问题 . 问题是:

System.Windows.Data错误:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1'' . BindingExpression:路径= VerticalContentAlignment;的DataItem = NULL; target元素是'ListViewItem'(Name ='');目标属性是'VerticalContentAlignment'(类型'VerticalAlignment')System.Windows.Data错误:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1'' . BindingExpression:路径= HorizontalContentAlignment;的DataItem = NULL; target元素是'ListViewItem'(Name =''); target属性是'HorizontalContentAlignment'(类型'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>

1 回答

  • 0

    我无法重现你的错误 . 它工作正常 . 在我看来,您在视图模型的属性上有一些不正确的初始化 . 请参阅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>
    

    请注意我已将 DataContext 绑定到 StackPanel . 所以我应该输入 StackPanel 作为 AncestorType

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

相关问题