[英]WPF BindingExpression path error
I have model BaseModel
with fields as Id
, Created
, Deleted
and Name
etc. 我有
BaseModel
模型,其字段为Id
, Created
, Deleted
和Name
等。
From this model I have derived models Category
and Brand
. 从这个模型中,我得出了
Category
和Brand
模型。 Model Brand
has field Image
. 模特
Brand
有现场Image
。
Also I have class Node
(Title as Name and Value as all object) 我也有
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));
}
}
}
I use class Node
for ComboBox
. 我对
ComboBox
使用Node
类。 So I have ComboBox for Category and for Brand. 所以我有用于类别和品牌的ComboBox。 Cause Category and Brand are derived from BaseModel I use for them same class
Node
原因Category和Brand源自我使用的BaseModel相同的类
Node
In <ComboBox.ItemTemplate>
I want to display Image
if it exists. 我想在
<ComboBox.ItemTemplate>
中显示Image
如果存在)。 So I wrote next code: 所以我写了下一个代码:
<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>
It works, it displays images only for Brand
items, cause only they have Image
. 它有效,它仅显示
Brand
商品的图像,因为只有它们具有Image
。
But in output window I see next message: 但是在输出窗口中,我看到下一条消息:
System.Windows.Data Error: 40 : BindingExpression path error: 'Image' property not found on 'object' ''Category' (HashCode=56044044)'.
System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“类别”(HashCode = 56044044)“上找不到“图像”属性。 BindingExpression:Path=Value.Image.FileLocation;
BindingExpression:路径= Value.Image.FileLocation; DataItem='Node' (HashCode=65381042);
DataItem ='节点'(HashCode = 65381042); target element is 'Image' (Name='ImageCheckBox');
目标元素是'Image'(Name ='ImageCheckBox'); target property is 'Source' (type 'ImageSource')
target属性是'Source'(类型'ImageSource')
As I read before, any exception in Binding can have influence on WPF App performance. 如前所述,绑定中的任何异常都会影响WPF App的性能。 How can I solve this issue?
我该如何解决这个问题?
You can set the ItemTemplateSelector
of the ComboBox
to something like this: 您可以将
ComboBox
的ItemTemplateSelector
设置为如下所示:
public class CategoryBrandItemTemplateSelector : DataTemplateSelector
{
public DataTemplate CategoryItemTemplate { get; set; }
public DataTemplate BrandItemTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if(item is Category)
return CategoryItemTemplate;
if(item is Brand)
return BrandItemTemplate;
return base.SelectTemplate(item, container);
}
}
In XAML: 在XAML中:
<ComboBox>
<ComboBox.ItemTemplateSelector>
<local:CategoryBrandItemTemplateSelector BrandItemTemplate="{StaticResource BrandTemplate}"
CategoryItemTemplate="{StaticResource CategoryTemplate}" />
</ComboBox.ItemTemplateSelector>
</ComboBox>
Where BrandTemplate
is a DataTemplate
resource declared somewhere (this will be the same that you are now using as the ItemTemplate
) and the CategoryTemplate
will be the one without the Image
and the failing binding. 其中
BrandTemplate
是在某处声明的DataTemplate
资源(这与您现在用作ItemTemplate
),而CategoryTemplate
将是没有Image
和绑定失败的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.