ListBoxItem produces quot;System.Windows.Data Error: 4quot; binding error(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";绑定错误
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01