我有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
。所以我有类别和品牌的组合框。原因类别和品牌是从 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'); 目标属性是“源”(类型“ImageSource”)
正如我之前读到的,Binding 中的任何异常都会影响 WPF 应用程序的性能。我该如何解决这个问题?