2016-01-22 25 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

相關問題