首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView中的绑定问题

ListView中的绑定问题
EN

Stack Overflow用户
提问于 2015-11-02 06:29:12
回答 1查看 841关注 0票数 0

我得到的问题w.r.t绑定到ListView项目。问题是:

AncestorType='System.Windows.Controls.ItemsControl',System.Windows.Data错误:4:无法找到引用'RelativeSource FindAncestor,RelativeSource AncestorLevel=‘1’绑定的源代码。BindingExpression:Path=VerticalContentAlignment;DataItem=null;目标元素是'ListViewItem‘(Name='');目标属性是'VerticalContentAlignment’(键入'VerticalAlignment') AncestorType='System.Windows.Controls.ItemsControl',System.Windows.Data错误:4:无法找到引用'RelativeSource FindAncestor,RelativeSource AncestorLevel=‘1’绑定的源代码。BindingExpression:Path=HorizontalContentAlignment;DataItem=null;目标元素是'ListViewItem‘(Name='');目标属性是'HorizontalContentAlignment’(键入'HorizontalAlignment')

我有一个ListView,并且设置了ItemContainerStyle,但是我还是遇到了同样的问题。请帮帮忙

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2015-11-02 07:44:26

我不能重复你的错误。它可以正常工作。在我看来,您在视图模型的属性上有一些不正确的初始化。请参阅MVVM ListView的工作示例。

模型:

代码语言:javascript
复制
 public class Person
 {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public bool IsClickable { get; set; }
 }

ViewModel:

代码语言:javascript
复制
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
    } 

用您的特性查看

代码语言:javascript
复制
<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

代码语言:javascript
复制
 <CheckBox IsChecked="{Binding IsClickable}" CommandParameter="{Binding}" Command="{Binding DataContext.SomeClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}"  Margin="1"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33471906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档