【问题标题】:WPF BindingExpression path errorWPF BindingExpression 路径错误
【发布时间】:2016-02-25 09:48:34
【问题描述】:

我的模型BaseModel 的字段为IdCreatedDeletedName 等。

从这个模型我导出了模型CategoryBrand。模型 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 应该显示什么?文字?没有?默认图片?
  • 那么,对象CategoryImage 属性吗?
  • @SteffenWinkler,如果没有,那就什么都没有
  • @ChrisF, Category 没有 Image 属性

标签: c# wpf binding binding-expressions


【解决方案1】:

您可以将ComboBoxItemTemplateSelector 设置为这样的:

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);
    }
}

在 XAML 中:

<ComboBox>
    <ComboBox.ItemTemplateSelector>
        <local:CategoryBrandItemTemplateSelector BrandItemTemplate="{StaticResource BrandTemplate}"
                                                 CategoryItemTemplate="{StaticResource CategoryTemplate}" />
    </ComboBox.ItemTemplateSelector>
</ComboBox>

其中BrandTemplate 是在某处声明的DataTemplate 资源(这与您现在使用的ItemTemplate 相同),CategoryTemplate 将是没有Image 和失败绑定的资源.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-02-13
    • 2015-08-11
    • 2010-10-12
    相关资源
    最近更新 更多