【发布时间】:2016-02-25 09:48:34
【问题描述】:
我的模型BaseModel 的字段为Id、Created、Deleted 和Name 等。
从这个模型我导出了模型Category 和Brand。模型 Brand 具有字段 Image。
我还有 Node 类(标题作为名称,值作为所有对象)
public class Node : INotifyPropertyChanged
{
private string _title;
private BaseModelDto _value;
private bool _isSelected;
#region ctor
public Node(string title, BaseModelDto value)
{
Title = title;
Value = value;
}
#endregion
#region Properties
public string Title
{
get { return _title; }
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public BaseModelDto Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChanged("Value");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我将Node 用于ComboBox。所以我有类别和品牌的组合框。原因 Category 和 Brand 派生自 BaseModel 我为它们使用相同的类Node
在<ComboBox.ItemTemplate> 中,如果存在Image,我想显示它。于是我写了下一段代码:
<Image MaxHeight="30" Margin="15,0,0,0" HorizontalAlignment="Right" Name="ImageCheckBox" Grid.Column="1">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding Value.Image.FileLocation, Converter={StaticResource ImagePathConverter}}" />
</Style>
</Image.Style>
</Image>
它有效,它只显示Brand 项目的图像,因为只有它们有Image。
但在输出窗口中我看到下一条消息:
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“类别”(HashCode=56044044)上找不到“图像”属性。 BindingExpression:Path=Value.Image.FileLocation; DataItem='节点' (HashCode=65381042);目标元素是 'Image' (Name='ImageCheckBox');目标属性是'Source'(类型'ImageSource')
正如我之前读到的,Binding 中的任何异常都会影响 WPF 应用程序的性能。我该如何解决这个问题?
【问题讨论】:
-
如果不是
Brand应该显示什么?文字?没有?默认图片? -
那么,对象
Category有Image属性吗? -
@SteffenWinkler,如果没有,那就什么都没有
-
@ChrisF,
Category没有Image属性
标签: c# wpf binding binding-expressions