我正在遇到一個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確實所示的值,HorizontalTextAlignment引發錯誤在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.HorizontalAlignment)和一個值(我默認設置一個,這是一個枚舉,所以「沒有機會」爲空)。
我還用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)
{
}
}
非常感謝!
能否請您包括後端 –
相關的代碼看起來工作得很好。你如何構建集合? –
也許你的問題出現,因爲你正在做一個綁定到界面?看看這個問題,它可能是有幫助的http://stackoverflow.com/questions/327984/wpf-databinding-to-interface-and-not-actual-object-casting-possible – lena