ListViewItem的绑定错误

时间:2015-03-15 14:31:14

标签: wpf binding

在应用程序运行时,我遇到以下错误: System.Windows.Data错误:4:无法找到与引用绑定的源' RelativeSource FindAncestor,AncestorType =' System.Windows.Controls.ItemsControl',AncestorLevel =' 1' &#39 ;. BindingExpression:路径= Horizo​​ntalContentAlignment;的DataItem = NULL;目标元素是' ListViewItem' (名称='&#39); target属性是' Horizo​​ntalContentAlignment' (键入' Horizo​​ntalAlignment')

我尝试添加以下样式代码行,但遗憾的是它没有帮助。 有什么想法吗?

代码:

<ListView Visibility="{Binding Path=IsListDisplayed, Converter={StaticResource BooleanToVisConverter}}" 
              ScrollViewer.VerticalScrollBarVisibility="Auto"                      
              ScrollViewer.HorizontalScrollBarVisibility="Disabled"                  
              Grid.Row="1" 
              ItemsSource="{Binding JobsList}"
              SelectedItem="{Binding SelectedJob}"                      
              x:Name="JobsLv">

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>                    
                <avt_controls:VirtualizingWrapPanel/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>                
            <DataTemplate>
                <Border BorderThickness="4" CornerRadius="10" Margin="10">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding JobStatus}" Value="Ready">
                                    <Setter Property="BorderBrush" Value="Green"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding JobStatus}" Value="FromMIS">
                                    <Setter Property="BorderBrush" Value="Blue"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding JobStatus}" Value="InProgress">
                                    <Setter Property="BorderBrush" Value="Red"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding JobName}" Grid.Row="0" FontSize="13" Margin="5,0,0,0"/>
                        <TextBlock Text="{Binding LastUpdate, StringFormat={}{0:dd/MM/yyyy hh:mm}}" Grid.Row="1" FontSize="13" Margin="5,0,0,0"/>
                        <ListView Grid.Row="2" ItemsSource="{Binding Designs}" BorderBrush="Transparent" Background="Transparent" Margin="3">
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Image Width="100" Margin="1">
                                        <Image.Style>
                                            <Style TargetType="{x:Type Image}">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsDownloading}" Value="True">
                                                        <Setter Property="Source" Value="{Binding ThumbnailFilePath}"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsDownloading}" Value="False">
                                                        <Setter Property="Source" Value="{Binding ThumbnailFilePath}"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </Border>
            </DataTemplate>                   
        </ListView.ItemTemplate>            
    </ListView>

1 个答案:

答案 0 :(得分:0)

(我知道答案较晚,但这是我找到的第一个链接,因此可能对某人有帮助。)

对于具有复杂 ItemTemplate 的列表视图,我在使用简单的 WrapPanel 时遇到了与我的 ItemsPanel 相同的问题(不过,任何地方都没有奇怪的对齐绑定)。在创建带有 ListView 的页面时,它会抱怨 Horizo​​ntalContentAlignment 和 VerticalContentAlignment 的绑定错误,之后它会正常工作。

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <WrapPanel Orientation="Horizontal"/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>

我用 https://www.nuget.org/packages/VirtualizingWrapPanel/ 替换了 WrapPanel,因为我无论如何都想虚拟化。现在错误消失了。请注意,出于某种原因,Orientation 与标准 WrapPanel 的工作方式相反。

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <wpftk:VirtualizingWrapPanel Orientation="Vertical"/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>