2016-01-22 93 views
0

我正在遇到一个ItemsControl的ItemTemplate非常奇怪的行为。一些BindingExperssions返回null,尽管DataContext。WPF:BindingExpression返回null,虽然源属性有一个值

这里是有问题的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> 

因此,例如,我可以看到,从视觉DataContext的内容属性检索。我期望在screen.However确实所示的值,Horizo​​ntalTextAlignment引发错误在Visual Studio中的输出窗口:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=HorizontalTextAlignment; DataItem='IndexCell' (HashCode=40508359); target element is 'TextBlock' (Name=''); target property is 'HorizontalAlignment' (type 'HorizontalAlignment') 

在DataContext对象的属性具有右式(System.Windows.Horizo​​ntalAlignment)和一个值(我默认设置一个,这是一个枚举,所以“没有机会”为空)。

我还用IValueConverter做了一个更多的测试。当在绑定中指定Path时,转换器确实会收到null。当没有路径时,它会接收整个对象,并对其进行调试,并且它具有该属性的值。感觉好像WPF中有一些错误,但我真的希望我错了。

有没有人知道为什么以及如何发生这种情况?也许我可以如何补救这一点?

EDIT(如需要): 这里是DataContext对象

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; } 
} 

而ItemsSource属性从视图模型的接口:

private IReadOnlyList<ICell> _index; 
    public IReadOnlyList<ICell> Index 
    { 
     get { return _index; } 
     private set 
     { 
      if (_index == value) 
       return; 
      _index = value; 
      RaisePropertyChanged(); 
     } 
    } 

EDIT2: 这里被BasicCell

的代码
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) 
    { 
    } 
} 

和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) 
    { 
    } 
} 

非常感谢!

+1

能否请您包括后端 –

+0

相关的代码看起来工作得很好。你如何构建集合? –

+0

也许你的问题出现,因为你正在做一个绑定到界面?看看这个问题,它可能是有帮助的http://stackoverflow.com/questions/327984/wpf-databinding-to-interface-and-not-actual-object-casting-possible – lena

回答

0

我可以创建自己的异常,唯一的方法是这样的

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') 

看枚举属性可以返回比枚举指定的其他值。例如尝试将任何整数值转换为任何枚举。

public HorizontalAlignment HorizontalTextAlignment 
    { 
     get 
     { 
      return (HorizontalAlignment)100; 
     }    
    } 

这将工作正常,您将在运行时(与您的)相同的绑定错误。我不知道你的班级层次,所以不能肯定地说。但是您在GetProperty(...)方法中使用泛型,因此可以返回null。你可以肯定地知道的唯一方法就是通过调试它是如何来的< null>

+0

感谢您的尝试,但您的代码并不反映我拥有的那个。 – user1509777