WPF绑定错误

摘要:
列表框绑定遇到如下奇怪错误:System。窗户。DataError:4:找不到引用为“RelativeSourceFindAncestor,AncestorType='System”的绑定的源。窗户。控制。ItemsControl',AncestorLevel=“1”。绑定Expressi

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>

  

免责声明:文章转载自《WPF绑定错误》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DevExpress WPF v20.1:全新升级Map、Pivot Grid控件功能es启动失败下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF 自定义按钮 Style

<Style TargetType="{x:Type Button}" x:Key="DefaultButton"> <Setter Property="Foreground" Value="White"/> <Setter Property="...

WPF-带有GridView的ListView样式

ListView是展示数据的常用控件,这里简单对带有GridView的ListView样式进行设置。 <Style TargetType="{x:Type ListViewItem}"> <Style.Triggers> <Trigger Property="IsSel...

【WPF】软件更新程序的设计思路

目标:客户端程序在启动时,自动联网检查服务端是否有新的版本,有则提示用户更新客户端。 思路: 1、打开Visual Studio,在主体程序的解决方案下再新建一个叫自动更新程序的项目。主体程序的目录是D:workspaceMyProjectClient,自动更新程序的目录是D:workspaceMyProjectUpdateTool。 2、最后打包出来时将...

c# wpf 条状刻度线,仪表盘的做法

网上看到 https://www.cnblogs.com/congqiandehoulai/p/12733245.html  照着例子做,一直不行,最后发现了问题。 1 需要添加两个引用 Microsoft.Expression.ControlsMicrosoft.Expression.Drawing 这两个dll需要引用到项目里,可以在自己的电脑里查到...

WPF 制作圆角按钮

在程序对应坐置插入以下代码,或是先拖一个按钮控件到窗体中,再替换对应的代码。 修改CornerRadius="18,3,18,3" 就可以改变圆角大小 按钮效果: <Button Content="Button" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top...

WPF数据双向绑定

设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人。 即:PropertyChanged(this, new PropertyChangedEventArgs("Age")) ,PropertyChangedEvent...