【发布时间】:2011-10-21 00:25:05
【问题描述】:
我知道有关于这个错误的问题,我找到了一些并阅读了它们,但老实说,我什么都不懂。
我有一个带有两个数据绑定 ListView 的 WPF 窗口。一个绑定到业务对象(我的自定义类),另一个绑定到Dictionary<string, string>。运行时一切似乎都正常,但在输出窗口中出现错误:
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')
VerticalContentAlignment 也是如此。
尽管 bost ListViews 按预期填充了项目,但它实际上在加载窗口时会导致明显的延迟。
在寻找答案时,我找到了这个线程 http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768 - 我实施了建议的解决方案,在两个 ListViews 中都提供了 HorizontalContentAlignment 和 VerticalContentAlignment 的默认值。它没有帮助。
这是 XAML:
-
列表视图 1:
<ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="13" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> <ListView.Resources> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> </Style> </ListView.Resources> <ListView.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="3" /> </ItemsPanelTemplate> </ListView> -
列表视图 2
<ListView.Resources> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> <EventSetter Event="Selected" Handler="lvItemSelected" /> </Style> <Style x:Key="GrayOutMappedColumn" TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding Mapped}" Value="False"> <Setter Property="TextElement.Foreground" Value="Black" /> </DataTrigger> </Style.Triggers> <Setter Property="TextElement.Foreground" Value="DarkGray" /> </Style> </ListView.Resources> <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <Border BorderBrush="LightGray" BorderThickness="0,0,0,1"> <TextBlock FontSize="12" FontWeight="Bold" Margin="0,10" Text="{Binding Name}" /> </Border> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> <ListView.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="4" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemTemplate> <DataTemplate> <StackPanel ClipToBounds="False" HorizontalAlignment="Stretch" Width="Auto"> <TextBlock HorizontalAlignment="Stretch" Style="{StaticResource GrayOutMappedColumn}" Text="{Binding Path=FriendlyName}" Width="Auto" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate>
数据绑定代码:
lvLanguageCodes.ItemsSource = languages;
lvLanguageCodes.SelectedValuePath = "Key";
lvLanguageCodes.DisplayMemberPath = "Value";
2:
lvDataTypes.ItemsSource = AssignDataType.datatypes;
其中datatypes 是ObservableCollection<Gate>,其中Gate 是我的商务舱(实现INotifyPropertyChanged、IComparable,除此之外没有什么特别之处)。
为什么我会收到错误消息?如果我明确设置它们的值,为什么它试图将这些对齐属性绑定到任何东西?
【问题讨论】:
-
为什么要用样式来设置ListView的一些属性?这将作为共享资源或类似资源很有用。直接在 ListView 元素上声明它们。
-
我在您的任何 XAML 中都看不到
RelativeSource绑定。可以发一下吗? -
这就是我要找的,但根本没有! XAML 甚至根本不包含字符串“relat”(不区分大小写)
-
您的问题解决了吗?我现在正在为同样的事情而苦苦挣扎,没有运气
-
@XMight 抱歉,已经过了两年多(还有许多其他问题):)我简直记不起来了。祝你好运
标签: wpf xaml data-binding listview binding