无法找到与引用绑定的源...数据绑定ListView问题

Kon*_*ski 5 data-binding wpf xaml binding listview

我知道有关于这个错误的问题,我找到了一些并阅读了它们,但说实话,我不明白一件事.

我有一个带有两个数据绑定ListViews的WPF窗口.一个绑定到业务对象(我的自定义类),另一个绑定到a 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 - 我实现了建议的解决方案,提供了默认值两个HorizontalContentAlignmentVerticalContentAlignment两个ListViews.它没有帮助.

这是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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 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>
    
    Run Code Online (Sandbox Code Playgroud)

数据绑定代码:

lvLanguageCodes.ItemsSource = languages;
lvLanguageCodes.SelectedValuePath = "Key";
lvLanguageCodes.DisplayMemberPath = "Value";
Run Code Online (Sandbox Code Playgroud)

2:

lvDataTypes.ItemsSource = AssignDataType.datatypes;
Run Code Online (Sandbox Code Playgroud)

这里datatypesObservableCollection<Gate>,这里Gate是我的类(实现INotifyPropertyChanged,IComparable并没有什么特别之处,否则).

为什么我收到错误?如果我明确设置它们的值,为什么要尝试将这些对齐属性绑定到任何东西?

Cha*_*lie 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}"
Run Code Online (Sandbox Code Playgroud)