ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误

ListBoxItem produces quot;System.Windows.Data Error: 4quot; binding error(ListBoxItem 产生“System.Windows.Data 错误:4;绑定错误)

本文介绍了ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This will produce at runtime the fallowing Line in the OutputWindow of 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='');

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

Can someone give me a tip, how I can solve this?

更新:

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

I have added the Properties to the style to try to eliminate the warning/error.

推荐答案

解决这个问题的最简单方法是确保你的 Listbox 有一个 ItemContainerStyle.请参阅以下示例:

The easiest way to solve this is to ensure that your Listbox has a ItemContainerStyle. See the following example:

<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>

您的项目正在创建中,默认情况下它们会查找未定义的父级属性.明确定义它会解决这个问题.

What happens is that your Items are being created, and by default they look for parent's property which isn't defined. Explicitly defining it will solve this problem.

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

I had the same issue using a TreeView and changing the bound source for these templates would cause those warnings.

这篇关于ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误

基础教程推荐