我正在遇到一个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