C# WPF:BindingExpression返回null,尽管源属性有一个值

C# WPF:BindingExpression返回null,尽管源属性有一个值,c#,wpf,data-binding,datatemplate,itemscontrol,C#,Wpf,Data Binding,Datatemplate,Itemscontrol,我在ItemsControl的ItemTemplate中遇到了一个非常奇怪的行为。有些BindingExpersions返回null,但DataContext是空的 下面是讨论中的XAML: <ItemsControl ItemsSource="{Binding Path=Index}" Grid.Row="1" Grid.Column="0" x:Name="RowsControl"> <ItemsControl.ItemsPanel&

我在ItemsControl的ItemTemplate中遇到了一个非常奇怪的行为。有些BindingExpersions返回null,但DataContext是空的

下面是讨论中的XAML:

<ItemsControl ItemsSource="{Binding Path=Index}" Grid.Row="1" Grid.Column="0" x:Name="RowsControl">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate DataType="{x:Type models:IndexCell}">
                            <Border Style="{StaticResource CellBorderStyle}"
                                    Background="{Binding BackgroundColor, Converter={StaticResource ColorToBrushConverter}}"
                                    ToolTip="{Binding Path=ToolTip}">

                                <TextBlock Text="{Binding Path=Content}" MouseDown="TextBlock_MouseDown"
                                           HorizontalAlignment="{Binding Path=HorizontalTextAlignment}"
                                           VerticalAlignment="Center"/>
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
和ViewModel中的ItemsSource属性:

private IReadOnlyList<ICell> _index;
    public IReadOnlyList<ICell> Index
    {
        get { return _index; }
        private set
        {
            if (_index == value)
                return;
            _index = value;
            RaisePropertyChanged();
        }
    }
private-IReadOnlyList\u索引;
公共IReadOnlyList索引
{
获取{return_index;}
专用设备
{
如果(_index==值)
返回;
_指数=价值;
RaisePropertyChanged();
}
}
编辑2: 这是Basicell的代码

public abstract class BasicCell : BasicProxyElement, ICell
{

    [StyleProperty]
    public Color BackgroundColor
    {
        get
        {
            return this.GetProperty<Color>("BackgroundColor", Color.FromArgb(0, 0, 0, 0));
        }
        set
        {
            if (value == BackgroundColor)
            {
                return;
            }

            if (this.TrySetProperty("BackgroundColor", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public string Content
    {
        get
        {
            return this.GetProperty<string>("Content", "");
        }
        set
        {
            if (value == Content)
            {
                return;
            }

            if (this.TrySetProperty("Content", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public double FontSize
    {
        get
        {
            return this.GetProperty<double>("FontSize", 11d);
        }
        set
        {
            if (value == FontSize)
            {
                return;
            }

            if (this.TrySetProperty("FontSize", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public FontWeight FontWeight
    {
        get
        {
            return this.GetProperty<FontWeight>("FontWeight", FontWeights.Normal);
        }
        set
        {
            if (value == FontWeight)
            {
                return;
            }

            if (this.TrySetProperty("FontWeight", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public HorizontalAlignment HorizontalTextAlignment
    {
        get
        {
            return this.GetProperty<HorizontalAlignment>("HorizontalTextAlignment", HorizontalAlignment.Center);
        }
        set
        {
            if (value == HorizontalTextAlignment)
            {
                return;
            }

            if (this.TrySetProperty("HorizontalTextAlignment", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public Color TextColor
    {
        get
        {
            return this.GetProperty<Color>("TextColor", Color.FromArgb(255, 0, 0, 0));
        }
        set
        {
            if (value == TextColor)
            {
                return;
            }

            if (this.TrySetProperty("TextColor", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public TextWrapping TextWrapping
    {
        get
        {
            return this.GetProperty<TextWrapping>("TextWrapping", TextWrapping.Wrap);
        }
        set
        {
            if (value == TextWrapping)
            {
                return;
            }

            if (this.TrySetProperty("TextWrapping", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public string ToolTip
    {
        get
        {
            return this.GetProperty<string>("ToolTip", null);
        }
        set
        {
            if (value == ToolTip)
            {
                return;
            }

            if (this.TrySetProperty("ToolTip", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }
    [StyleProperty]
    public VerticalAlignment VerticalTextAlignment
    {
        get
        {
            return this.GetProperty<VerticalAlignment>("VerticalTextAlignment", VerticalAlignment.Center);
        }
        set
        {
            if (value == VerticalTextAlignment)
            {
                return;
            }

            if (this.TrySetProperty("VerticalTextAlignment", value))
            {
                this.RaisePropertyChanged();
            }
        }
    }

    public BasicCell(IGraphElement dataElement, IProxyGraph graph, string visualTarget) : base(dataElement, graph, visualTarget)
    {
    }
}
公共抽象类BasicCell:BasicProxyElement,ICell { [样式属性] 公共色背景色 { 得到 { 返回这个.GetProperty(“BackgroundColor”,Color.FromArgb(0,0,0,0)); } 设置 { 如果(值==背景色) { 返回; } if(this.TrySetProperty(“BackgroundColor”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共字符串内容 { 得到 { 返回此.GetProperty(“Content”和“”); } 设置 { 如果(值==内容) { 返回; } if(this.TrySetProperty(“Content”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共双字号 { 得到 { 返回此.GetProperty(“FontSize”,11d); } 设置 { 如果(值==FontSize) { 返回; } if(this.TrySetProperty(“FontSize”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公众体重 { 得到 { 返回此.GetProperty(“FontWeight”,FontWeights.Normal); } 设置 { 如果(值==FontWeight) { 返回; } if(this.TrySetProperty(“FontWeight”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共水平对齐水平文本对齐 { 得到 { 返回此.GetProperty(“HorizontalTextAlignment”,HorizontalAlignment.Center); } 设置 { 如果(值==水平文本对齐) { 返回; } if(this.TrySetProperty(“HorizontalTextAlignment”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共颜色文本颜色 { 得到 { 返回这个.GetProperty(“TextColor”,Color.FromArgb(255,0,0,0)); } 设置 { 如果(值==TextColor) { 返回; } if(this.TrySetProperty(“TextColor”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共文本包装文本包装 { 得到 { 返回此.GetProperty(“TextWrapping”,TextWrapping.Wrap); } 设置 { 如果(值==TextWrapping) { 返回; } if(this.TrySetProperty(“TextWrapping”,value)) { this.RaisePropertyChanged(); } } } [样式属性] 公共字符串工具提示 { 得到 { 返回此.GetProperty(“工具提示”,null); } 设置 { 如果(值==工具提示) { 返回; } if(this.TrySetProperty(“工具提示”,值)) { this.RaisePropertyChanged(); } } } [样式属性] 公共垂直对齐垂直文本对齐 { 得到 { 返回此.GetProperty(“VerticalTextAlignment”,VerticalAlignment.Center); } 设置 { 如果(值==垂直文本对齐) { 返回; } if(this.TrySetProperty(“VerticalTextAlignment”,value)) { this.RaisePropertyChanged(); } } } public BasicCell(IGraphElement数据元素、IProxyGraph图形、字符串visualTarget):基本(数据元素、图形、visualTarget) { } } 以及IndexCell:

public class IndexCell : BasicCell
{
    private static readonly IProxyElementFactory _factory = new DelegateProxyElementFactory("IndexCell",
        (graphElement, controller, visualTarget) => new IndexCell(graphElement, controller, visualTarget));

    public static IProxyElementFactory Factory
    {
        get { return _factory; }
    }

    private static readonly IStyleProvider _styleProvider = new ReflectionDefaultStyleProvider<IndexCell>();

    public static IStyleProvider StyleProvider
    {
        get { return _styleProvider; }
    }

    public IndexCell(IGraphElement dataElement, IProxyGraph graph, string visualTarget) : base(dataElement, graph, visualTarget)
    {
    }
}
公共类IndexCell:BasicCell
{
私有静态只读IProxyElementFactory _factory=新的DelegateProxyElementFactory(“IndexCell”,
(GraphiElement,controller,visualTarget)=>新的IndexCell(GraphiElement,controller,visualTarget));
公共静电IProxyElementFactory工厂
{
获取{return\u factory;}
}
私有静态只读IStyleProvider _styleProvider=新ReflectionDefaultStyleProvider();
公共静态IStyleProvider样式提供程序
{
获取{return\u styleProvider;}
}
public IndexCell(IGraphElement数据元素、IProxyGraph图形、字符串visualTarget):基(数据元素、图形、visualTarget)
{
}
}

多谢各位

我创建您的异常的唯一方法是这样

public interface ICell
{
    string Content { get; set; }
    Color TextColor { get; set; }
    Color BackgroundColor { get; set; }
    double FontSize { get; set; }
    FontWeight FontWeight { get; set; }
    TextWrapping TextWrapping { get; set; }
    HorizontalAlignment? HorizontalTextAlignment { get; set; }
    VerticalAlignment VerticalTextAlignment { get; set; }
    string ToolTip { get; set; }
}

public class IndexCell : ICell
{
   public string Content { get; set; }
   public Color TextColor { get; set; }
   public Color BackgroundColor { get; set; }
   public double FontSize { get; set; }
   public FontWeight FontWeight { get; set; }
   public TextWrapping TextWrapping { get; set; }
   public HorizontalAlignment? HorizontalTextAlignment { get; set; }
   public VerticalAlignment VerticalTextAlignment { get; set; }
   public string ToolTip { get; set; }
}
因此,请确保您在IndexCell中使用的类型的属性。某种程度上,可以包含null的类型正在到达绑定

我的例外:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=HorizontalTextAlignment; DataItem='IndexCell' (HashCode=55786324); target element is 'TextBlock' (Name=''); target property is 'HorizontalAlignment' (type 'HorizontalAlignment')
这将很好地工作,您将在运行时收到一个绑定错误(与您的一样)。我不知道你们的班级结构,所以不能肯定。但是您在这里使用的是GetProperty(…)方法中的泛型,因此它可能返回null。你能确定的唯一方法是通过调试它的comi
public interface ICell
{
    string Content { get; set; }
    Color TextColor { get; set; }
    Color BackgroundColor { get; set; }
    double FontSize { get; set; }
    FontWeight FontWeight { get; set; }
    TextWrapping TextWrapping { get; set; }
    HorizontalAlignment? HorizontalTextAlignment { get; set; }
    VerticalAlignment VerticalTextAlignment { get; set; }
    string ToolTip { get; set; }
}

public class IndexCell : ICell
{
   public string Content { get; set; }
   public Color TextColor { get; set; }
   public Color BackgroundColor { get; set; }
   public double FontSize { get; set; }
   public FontWeight FontWeight { get; set; }
   public TextWrapping TextWrapping { get; set; }
   public HorizontalAlignment? HorizontalTextAlignment { get; set; }
   public VerticalAlignment VerticalTextAlignment { get; set; }
   public string ToolTip { get; set; }
}
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=HorizontalTextAlignment; DataItem='IndexCell' (HashCode=55786324); target element is 'TextBlock' (Name=''); target property is 'HorizontalAlignment' (type 'HorizontalAlignment')
 public HorizontalAlignment HorizontalTextAlignment
    {
        get
        {
            return (HorizontalAlignment)100;
        }            
    }