1

I have model BaseModel with fields as Id, Created, Deleted and Name etc.

From this model I have derived models Category and Brand. Model Brand has field Image.

Also I have class Node (Title as Name and Value as all object)

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. So I have ComboBox for Category and for Brand. Cause Category and Brand are derived from BaseModel I use for them same class Node

In <ComboBox.ItemTemplate> I want to display Image if it exists. 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.

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)'. BindingExpression:Path=Value.Image.FileLocation; DataItem='Node' (HashCode=65381042); target element is 'Image' (Name='ImageCheckBox'); target property is 'Source' (type 'ImageSource')

As I read before, any exception in Binding can have influence on WPF App performance. How can I solve this issue?

Yael
  • 1,566
  • 3
  • 18
  • 25
demo
  • 6,038
  • 19
  • 75
  • 149

1 Answers1

0

You can set the ItemTemplateSelector of the ComboBox to something like this:

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:

<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.

Szabolcs Dézsi
  • 8,743
  • 21
  • 29