无法找到与参考' RelativeSource FindAncestor

时间:2017-11-20 11:30:48

标签: wpf xaml data-binding

我正在使用复合收藏:

  • Comboboxitem含量"选择供应商"
  • Collectioncontainer绑定到供应商对象的Observablecollection

所需功能:用户必须从组合框中选择供应商。选择"选择供应商"将viewmodel中的Vendor属性设置为null。

我收到了绑定错误。任何想法如何解决这个问题?

调试时出错

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 'ComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

XAML

<ComboBox Name='cmbVendor'
            SelectedItem='{Binding Vendor, Converter={StaticResource ComboboxConverter}, Mode=TwoWay}'
            IsSynchronizedWithCurrentItem='True'>
    <ComboBox.Resources>
      <CollectionViewSource x:Key='VendorsCollection'
                            Source='{Binding Vendors}' />
      <DataTemplate DataType='{x:Type ComboBoxItem}'>
        <TextBlock Text='{Binding Content}' />
      </DataTemplate>
      <DataTemplate DataType='{x:Type objects:Vendor}'>
        <StackPanel>
          <TextBlock Text='{Binding Name}' />
        </StackPanel>
      </DataTemplate>
    </ComboBox.Resources>
    <ComboBox.ItemsSource>
      <CompositeCollection>
        <ComboBoxItem Content='Select a vendor' />
        <CollectionContainer Collection='{Binding Source={StaticResource VendorsCollection}}' />
      </CompositeCollection>
    </ComboBox.ItemsSource>
  </ComboBox>

ComboboxConverter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

      var vendor = value as Vendor;
        if (vendor != null)
        {
            return vendor;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var vendor = value as Vendor;
        if (vendor != null)
        {
            return vendor;
        }

        var comboboxItem = value as ComboBoxItem;
        if (comboboxItem != null)
        {
            return null;
        }
        return null;
    }

3 个答案:

答案 0 :(得分:1)

这些绑定错误是无害的,并在内部处理,因此您可以安全地忽略它们或抑制它们。有关更多信息,请参阅以下链接。

解决WPF中无害的绑定错误: https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf

答案 1 :(得分:1)

添加到您的 App.xaml

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

答案 2 :(得分:0)

添加到您的 App.xaml

       'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '90daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:pageviews'}, {'expression': 'ga:totalEvents'}],
          'dimensions': [{'name': 'ga:pagePathlevel1'}, {'name': 'ga:eventCategory'}],
            "orderBys": [
                {
                    "orderType": "VALUE",
                    "sortOrder": "DESCENDING",
                    "fieldName": "ga:totalEvents"
                }
            ],
            "dimensionFilterClauses": [
                {
                    "filters": [
                        {
                            "dimensionName": "ga:eventCategory",
                            "operator": "EXACT",
                            "not": "true",
                            "expressions": ["specific_events"]
                        }
                    ]
                }
            ],
            "pageSize": 10
            }
          ]
        }

并使用它:

<!-- https://stackoverflow.com/questions/47391020/cannot-find-source-for-binding-with-reference-relativesource-findancestor -->
<Style 
    x:Key="BugFreeListViewItemStyle"
    TargetType="{x:Type ListViewItem}"
    >
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>