zoukankan      html  css  js  c++  java
  • WPF绑定错误

    listbox绑定遇到了奇怪的报错如下:

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

    分析下,我listboxitem根本就没有设置这些啊,经过不断摸索发现,应该是默认的样式,wpf的listboxitem默认样式如下:

    Style
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
           <ResourceDictionary/>
        </Style.Resources>
        <Setter Property="Panel.Background">
           <Setter.Value>
              <SolidColorBrush>
            #00FFFFFF
              </SolidColorBrush>
           </Setter.Value>
        </Setter>
        <Setter Property="Control.HorizontalContentAlignment">
           <Setter.Value>
              <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}"/>
           </Setter.Value>
        </Setter>
        <Setter Property="Control.VerticalContentAlignment">
           <Setter.Value>
              <Binding Path="VerticalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}"/>
           </Setter.Value>
        </Setter>
        <Setter Property="Control.Padding">
           <Setter.Value>
              <Thickness>
            2,0,0,0
              </Thickness>
           </Setter.Value>
        </Setter>
        <Setter Property="Control.Template">
           <Setter.Value>
              <ControlTemplate TargetType="{x:Type ListBoxItem}">
                 ...
              </ControlTemplate>
           </Setter.Value>
        </Setter>
     </Style>

    尝试如下覆盖原来的样式方法解决,发现还是一样报错

     <Style TargetType="ListBoxItem">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                            <Setter Property="VerticalContentAlignment" Value="Center" />
                                        </Style>

    ,最终发现要这样解决才可以

    <ListBox.Resources>
                                        <Style TargetType="ListBoxItem">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                            <Setter Property="VerticalContentAlignment" Value="Center" />
                                        </Style>
                                    </ListBox.Resources>
                                    <ListBox.ItemContainerStyle>
                                        <Style TargetType="ListBoxItem">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                            <Setter Property="VerticalContentAlignment" Value="Center" />
                                        </Style>
                                    </ListBox.ItemContainerStyle>
    

      

  • 相关阅读:
    EF CodeFirst下,当实体结构发生修改怎么更新数据库结构 【常用总结】
    基于 ASP.NET Core 的 EF Core 入门
    autofac 一个接口多个实现的情况
    C#中使用HttpClient来Post数据的内容HttpContent的各种格式
    # 使用HttpClient的post,get 封装
    C# 模拟提交带附件(input type=file)的表单
    C#使用HttpClient上传文件并附带其他参数
    2021年总结一下
    PhpStorm+Xdebug断点调试
    PHP数组排序
  • 原文地址:https://www.cnblogs.com/karl-F/p/8808621.html
Copyright © 2011-2022 走看看