我知道有关于这个错误的问题,我找到了一些并阅读了它们,但说实话,我不明白。
我有一个带有两个数据绑定ListViews的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 - 我实现了建议的解决方案,在两个ListView中提供了HorizontalContentAlignment
和VerticalContentAlignment
的默认值。它没有帮助。
这是XAML:
ListView 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>
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
,其他方面没什么特别的。)
为什么我收到错误?如果我明确设置它们的值,为什么要尝试将这些对齐属性绑定到任何东西?
答案 0 :(得分:6)
您需要覆盖默认的ItemContainerStyle
。所以Blam的答案是半正确的。这是我的:
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
...
</Style>
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"
答案 1 :(得分:1)
请尝试以下操作。不确定它是否适用于您的环境,但这是适用于我的语法。
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
这可能是一个数据绑定问题。你能试试吗?
<ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="1"
PresentationTraceSources.TraceLevel="High"
DisplayMemberPath="Value" SelectedValuePath="Key" ItemsSource="Langages">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.Resources>
</ListView>
你知道ListView.ItemsPanel没有关闭。我很惊讶这个编译。