I know that there have been questions regarding this error, I've found some and read them but to be honest, I didn't understand a thing.
我知道有关于这个错误的问题,我找到了一些并阅读了它们,但说实话,我不明白一件事。
I have a WPF window with two databound ListViews. One is bound to business objects (my custom classes), another to a Dictionary<string, string>
. Everything seems to look OK in runtime, but I'm getting errors in the Output window:
我有一个带有两个数据绑定ListViews的WPF窗口。一个绑定到业务对象(我的自定义类),另一个绑定到Dictionary
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')
System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''。 BindingExpression:路径= HorizontalContentAlignment;的DataItem = NULL; target元素是'ListViewItem'(Name =''); target属性是'HorizontalContentAlignment'(类型'HorizontalAlignment')
and the same for VerticalContentAlignment
.
和VerticalContentAlignment相同。
Even though bost ListViews get populated with items as expected, it actually causes a noticeable delay when loading the window.
尽管bost ListViews按预期填充了项目,但实际上在加载窗口时会导致明显的延迟。
Looking for an answer, I found this thread http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768 - and I implemented the suggested solution, supplying default values of both HorizontalContentAlignment
and VerticalContentAlignment
in both ListViews. It didn't help.
寻找答案,我找到了这个主题http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768 - 我实现了建议的解决方案,提供默认值两个ListView中的HorizontalContentAlignment和VerticalContentAlignment。它没有帮助。
Here's the XAML:
这是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>
ListView 1:
ListView 2
Data-binding code:
lvLanguageCodes.ItemsSource = languages;
lvLanguageCodes.SelectedValuePath = "Key";
lvLanguageCodes.DisplayMemberPath = "Value";
2:
lvDataTypes.ItemsSource = AssignDataType.datatypes;
where datatypes
is ObservableCollection<Gate>
, where Gate
is my business class (implementing INotifyPropertyChanged
, IComparable
and nothing special about it otherwise).
其中数据类型是ObservableCollection
Why am I getting an error? Why is it trying to bind these alignment properties to anything at all, if I set their values explicitly?
为什么我收到错误?如果我明确设置它们的值,为什么要尝试将这些对齐属性绑定到任何东西?
2 个解决方案
#1
6
You need to override the default ItemContainerStyle
. So Blam's answer is half-correct. Here is mine:
您需要覆盖默认的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}"
#2
1
Try below. Not sure it will work in your environment but this is the syntax that is working for me.
试试以下。不确定它是否适用于您的环境,但这是适用于我的语法。
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
It may be a a data binding issue. Could you try?
它可能是一个数据绑定问题。你能试试吗?
<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>
You do know ListView.ItemsPanel is not closed out. I am surprised this compiles.
你知道ListView.ItemsPanel没有关闭。我很惊讶这个编译。
#1
6
You need to override the default ItemContainerStyle
. So Blam's answer is half-correct. Here is mine:
您需要覆盖默认的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}"
#2
1
Try below. Not sure it will work in your environment but this is the syntax that is working for me.
试试以下。不确定它是否适用于您的环境,但这是适用于我的语法。
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
It may be a a data binding issue. Could you try?
它可能是一个数据绑定问题。你能试试吗?
<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>
You do know ListView.ItemsPanel is not closed out. I am surprised this compiles.
你知道ListView.ItemsPanel没有关闭。我很惊讶这个编译。