【问题标题】:ListBoxItem produces "System.Windows.Data Error: 4" binding errorListBoxItem 产生“System.Windows.Data Error: 4”绑定错误
【发布时间】:2011-04-26 20:51:30
【问题描述】:

我已经创建了休闲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 的 OutputWindow 中生成休闲线:

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

谁能给我一个提示,我该如何解决这个问题?

更新

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

【问题讨论】:

  • 你需要那种风格吗?为什么不能在数据模板中的 stackpanel/textblock 上设置对齐方式?

标签: .net wpf xaml listbox listboxitem


【解决方案1】:

这些HorizontalContentAlignmentVerticalContentAlignment 问题是由于容器未分别定义其HorizontalAlignmentVerticalAlignment 而引起的。

因此,我没有为ListBoxItemTreeViewItem 或更糟糕地直接设置这些属性创建通用样式,而是在我的App.xaml 中为ItemsControl 创建了一个通用样式,它解决了导致此问题的任何问题,例如这个:

<Style TargetType="{x:Type ItemsControl}">
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
</Style>

【讨论】:

    【解决方案2】:

    对我来说,罪魁祸首是 TreeView,而不是 ListView。我按照@bsegraves 的建议将全局样式添加到了我的 App.xaml:

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

    我还必须将以下内容添加到我的 TreeView:

    <TreeView.ItemContainerStyle>
        <Style
            BasedOn="{StaticResource {x:Type TreeViewItem}}"
            TargetType="{x:Type TreeViewItem}">         
        </Style>
    </TreeView.ItemContainerStyle>
    

    由于某种原因,BasedOn 属性是缺失的部分。 (我尝试了@Etienne 的方法,但没有奏效。)

    【讨论】:

      【解决方案3】:

      另一个对我有用的解决方法是通过在类的构造函数或顶级窗口中将数据绑定源开关级别设置为关键来抑制这些错误(实际上,将它们称为警告似乎更合适)-

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

      参考:How to suppress the System.Windows.Data Error warning message

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

      【讨论】:

      • 这是个坏主意。首先,错误仅显示在 Visual Studio 中,因此它不是用户面临的错误。其次,你会隐藏错误。错误的存在是有原因的,从长远来看,隐藏它(连同它的所有兄弟姐妹)很可能会让你变得更糟。修正错误,不要隐藏它!
      • 我有点不同意。我们是成年人,他们可以记住我们已经禁用了一段时间的错误,并在它对我们的应用程序的需求变得更重要时重新审视这些问题。您可以将错误视为对 WPF 协方差的粗略(但有用)排序的过度热心警告:您可以将 ListView 行绑定到派生较少的对象,并且很高兴拥有运行时的那些字段对象没有碰巧有,是空白的。
      • 一般来说隐藏错误似乎是错误的,因为有一种情况会吐出来。
      【解决方案4】:

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

      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>
      

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

      【讨论】:

        【解决方案5】:

        解决此问题的最简单方法是确保您的 Listbox 具有 ItemContainerStyle。请参阅以下示例:

        <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 时遇到了同样的问题,更改这些模板的绑定源会导致这些警告。

        【讨论】:

          猜你喜欢
          • 2018-08-09
          • 1970-01-01
          • 2011-02-06
          • 2012-11-10
          • 2019-02-16
          • 1970-01-01
          • 2012-08-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多