我正在为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 回答
我无法重现你的错误 . 它工作正常 . 在我看来,您在视图模型的属性上有一些不正确的初始化 . 请参阅MVVM ListView的工作示例 .
Model:
ViewModel:
View with your features:
请注意我已将
DataContext
绑定到StackPanel
. 所以我应该输入StackPanel
作为AncestorType
: