代码之家  ›  专栏  ›  技术社区  ›  BennoDual

ListBoxItem产生“System.Windows.Data Error:4”绑定错误

  •  27
  • BennoDual  · 技术社区  · 14 年前

    我创造了休耕地 ListBox :

    <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
      <ListBox.Resources>
          <Style TargetType="{x:Type ListBoxItem}"
                 BasedOn="{StaticResource {x:Type ListBoxItem}}">
              <Style.Triggers>
                  <!--This trigger is needed, because RelativeSource binding can only succeeds if the current ListBoxItem is already connected to its visual parent-->
                  <Trigger Property="IsVisible" Value="True">
                      <Setter Property="HorizontalContentAlignment"
                              Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                      <Setter Property="VerticalContentAlignment"
                              Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                  </Trigger>
              </Style.Triggers>
          </Style>
      </ListBox.Resources>
      <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
                    <TextBlock Text="{Binding Number}" />
                    <StackPanel Orientation="Vertical" Margin="7,0,0,0">
                        <TextBlock Text="{Binding File}" />
                        <TextBlock Text="{Binding Dir}" Foreground="DarkGray" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    这将在运行时生成VisualStudio输出窗口中的衰减线:

    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 'ListBoxItem' (Name='');
    

    更新 :

    我已将属性添加到样式中,以尝试消除警告/错误。

    3 回复  |  直到 12 年前
        1
  •  44
  •   Etienne    13 年前

    解决这一问题的最简单方法是确保 列表框 项目容器样式

    <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
            </Style>
        </ListBox.ItemContainerStyle>
    
    ...
    
    </ListBox>
    

    发生的情况是正在创建您的项,默认情况下,它们会查找未定义的父属性。明确定义它将解决这个问题。

    我在使用TreeView时遇到了同样的问题,更改这些模板的绑定源将导致这些警告。

        2
  •  24
  •   Community CDub    7 年前

    这里的答案为我解决了这个问题:

    ListBox with Grid as ItemsPanelTemplate produces weird binding errors

    定义一个顶级样式(在我的App.xaml中)来针对问题类型“修复”我的问题。以下是适合您的风格:

    <Style TargetType="{x:Type ListBoxItem}">
         <Setter Property="HorizontalContentAlignment" Value="Left" />
         <Setter Property="VerticalContentAlignment" Value="Top" />
    </Style>
    

    在我的例子中,我创建了一些TreeView项目,然后将我的TreeView绑定到创建的项目。发生绑定错误是因为在将TreeView项的绑定添加到TreeView之前正在解决它们。正确的解决方案是不创建TreeViewItem,而是创建一个包含所需数据(头和项)的类。只是转述一下我的情况,以防和你有相似之处。

        3
  •  0
  •   akjoshi HCP    11 年前

    #if DEBUG     
        System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level =
            System.Diagnostics.SourceLevels.Critical;
    #endif
    

    裁判。: How to suppress the System.Windows.Data Error warning message

    更新:这不是最好的解决方案,但对于有害的警告,这看起来很好。