ListBoxItem生成"System.Windows.Data Error:4"绑定错误

Ben*_*ual 29 .net wpf xaml listbox listboxitem

我创造了以下事件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>
Run Code Online (Sandbox Code Playgroud)

这将在运行时生成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='');
Run Code Online (Sandbox Code Playgroud)

有人可以给我一个提示,我怎么能解决这个问题?

更新:

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

Eti*_*nne 44

解决此问题的最简单方法是确保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>
Run Code Online (Sandbox Code Playgroud)

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

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


bse*_*ves 24

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

使用Grid作为ItemsPanelTemplate的ListBox会产生奇怪的绑定错误

定义一个顶级样式(在我的App.xaml中),针对问题类型"修复"我的问题.这是一个适合你的风格:

<Style TargetType="{x:Type ListBoxItem}">
     <Setter Property="HorizontalContentAlignment" Value="Left" />
     <Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
Run Code Online (Sandbox Code Playgroud)

在我的例子中,我创建了一些TreeViewItems然后将我的TreeView绑定到创建的项目.发生绑定错误是因为TreeViewItem的绑定在被添加到TreeView之前被解析.正确的解决方案是不创建TreeViewItem,而是创建一个包含我需要的数据的类(Header和Items).只是转发我的情况,以防与你自己的情况相提并论.