【问题标题】:Cannot find source for binding with reference... databound ListView problem找不到与参考绑定的源...数据绑定ListView问题
【发布时间】:2011-10-21 00:25:05
【问题描述】:

我知道有关于这个错误的问题,我找到了一些并阅读了它们,但老实说,我什么都不懂。

我有一个带有两个数据绑定 ListView 的 WPF 窗口。一个绑定到业务对象(我的自定义类),另一个绑定到Dictionary<string, string>。运行时一切似乎都正常,但在输出窗口中出现错误:

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 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

VerticalContentAlignment 也是如此。

尽管 bost ListViews 按预期填充了项目,但它实际上在加载窗口时会导致明显的延迟。

在寻找答案时,我找到了这个线程 http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768 - 我实施了建议的解决方案,在两个 ListViews 中都提供了 HorizontalContentAlignmentVerticalContentAlignment 的默认值。它没有帮助。

这是 XAML:

  • 列表视图 1:

                    <ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="13" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                        <ListView.Resources>
                            <Style TargetType="ListViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                <Setter Property="VerticalContentAlignment" Value="Stretch" />
                            </Style>
                        </ListView.Resources>
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3" />
                            </ItemsPanelTemplate>
                        </ListView>
    
  • 列表视图 2

                            <ListView.Resources>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="VerticalContentAlignment" Value="Stretch" />
                                    <EventSetter Event="Selected" Handler="lvItemSelected" />
                                </Style>
                                <Style x:Key="GrayOutMappedColumn" TargetType="{x:Type TextBlock}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Mapped}" Value="False">
                                            <Setter Property="TextElement.Foreground" Value="Black" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                    <Setter Property="TextElement.Foreground" Value="DarkGray" />
                                </Style>
                            </ListView.Resources>
                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <Border BorderBrush="LightGray" BorderThickness="0,0,0,1">
                                                <TextBlock FontSize="12" FontWeight="Bold" Margin="0,10" Text="{Binding Name}" />
                                            </Border>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Columns="4" />
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel ClipToBounds="False" HorizontalAlignment="Stretch" Width="Auto">
                                        <TextBlock HorizontalAlignment="Stretch" Style="{StaticResource GrayOutMappedColumn}" Text="{Binding Path=FriendlyName}" Width="Auto" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
    

数据绑定代码:

lvLanguageCodes.ItemsSource = languages;
lvLanguageCodes.SelectedValuePath = "Key";
lvLanguageCodes.DisplayMemberPath = "Value";

2:

lvDataTypes.ItemsSource = AssignDataType.datatypes;

其中datatypesObservableCollection&lt;Gate&gt;,其中Gate 是我的商务舱(实现INotifyPropertyChangedIComparable,除此之外没有什么特别之处)。

为什么我会收到错误消息?如果我明确设置它们的值,为什么它试图将这些对齐属性绑定到任何东西?

【问题讨论】:

  • 为什么要用样式来设置ListView的一些属性?这将作为共享资源或类似资源很有用。直接在 ListView 元素上声明它们。
  • 我在您的任何 XAML 中都看不到 RelativeSource 绑定。可以发一下吗?
  • 这就是我要找的,但根本没有! XAML 甚至根本不包含字符串“relat”(不区分大小写)
  • 您的问题解决了吗?我现在正在为同样的事情而苦苦挣扎,没有运气
  • @XMight 抱歉,已经过了两年多(还有许多其他问题):)我简直记不起来了。祝你好运

标签: wpf xaml data-binding listview binding


【解决方案1】:

您需要覆盖默认的ItemContainerStyle。所以 Blam 的回答是正确的。这是我的:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
    ...
</Style>

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"

【讨论】:

  • 我认为你是对的(已经有一段时间了,我不再使用那个应用程序了,所以我无法验证这一点),但我仍然想了解为什么.为什么必须覆盖ItemContainerStyle?那么ListViewItem 是干什么用的呢?
  • 当您为 ListViewItem 内联样式时,您不会覆盖整个默认样式。我的假设是在默认样式的某处,有一个 VerticalContentAlignment 的 RelativeSource 绑定(请参阅此处以获取默认样式的示例:msdn.microsoft.com/en-us/library/ms788747(v=vs.110).aspx)。
  • 感谢您的解释。天哪,我真的不会将 WPF 评为对程序员友好 :) 到底有没有人应该得出这样的结论,即原始错误消息的含义,而不知道 WPF 的内里外外!答案已接受
  • 是的,WPF 的学习曲线是荒谬的。它真的很深。
  • 我也遇到了和 OP 一样的问题。实现分组后,它开始出现在我的调试窗口中。 OverridesDefaultStyle 解决了它
【解决方案2】:

试试下面。不确定它是否适用于您的环境,但这是适合我的语法。

    <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
            </Style>

这可能是一个数据绑定问题。可以试试吗?

    <ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="1" 
              PresentationTraceSources.TraceLevel="High" 
              DisplayMemberPath="Value" SelectedValuePath="Key" ItemsSource="Langages">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.Resources>
    </ListView>

您知道 ListView.ItemsPanel 没有关闭。我很惊讶这个编译。

【讨论】:

    猜你喜欢
    • 2018-04-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    相关资源
    最近更新 更多