0

Ok so I have a WPF DataGrid bound to a collection of Products. The Product class has a property called ParentNumber and two navigation properties, Product Parent and ICollection<Product> SubProducts.

I want to indicate in my DataGrid with a small image what products are children and what products are parents.

In my DataGrid I have a column with two images. The first image has its visibility property bound to the parentNumber with a Converter that returns System.Windows.Visibility.Visible if the parentNumber is not null. That part was easy.

So my question is how I can do the same for the parent product? What do I bind to? Do I need to add another property in my class? What is the best way to do this?

I have implemented the INotifyPropertyChanged on all the properties in the Product class.

Here is the code:

<DataGridTemplateColumn Header="Productnr" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Height="16"
                       Margin="0,0,5,0"
                       Source="{StaticResource ChildImage}"
                       Visibility="{Binding Path=IsChild,
                                                 Mode=OneWay,
                                                 Converter={StaticResource BoolToVisibilityConverter}}" />
                <Image Height="16"
                       Margin="0,0,5,0"
                       Source="{StaticResource ParentImage}"
                       Visibility="{Binding ???? />
                <TextBlock Text="{Binding Path=ProductNumber}" />
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

public class Product : INotifyPropertyChanged
{
    public string ProductNumber { get; set; }

    public string ParentNumber
    {
        get
        {
            return _parentNumber;
        }
        set
        {
            _parentNumber = value;
            OnPropertyChanged("ParentNumber");
        }
    }

    public virtual Product Parent { get; set; }

    public virtual ICollection<Product> SubProducts
    {
        get
        {
            return _subProducts;
        }
        set
        {
            _subProducts = value;
            OnPropertyChanged("SubProducts");
        }
    }

    ...
}
Dux
  • 31
  • 4
  • Yes add a property to Product that identifies the if it is a Parent or not. And do you mean to have the existing visibility as TwoWay? How would the converter know what PartNumber to return? In product I would have two properties IsParent and IsChild. In my mind doing IsParent in the converter is business logic in the UI. – paparazzo Feb 12 '14 at 16:41
  • Ok I will test that and it makes sense. I just thought that I didnt need to add properties to the class for that purpose only since I allready have the parent and the childCollection. – Dux Feb 13 '14 at 09:19
  • If you already have it then it is not clear to me. Try another converter.? – paparazzo Feb 13 '14 at 13:56

1 Answers1

0

You could add a converter that checks if the subproduct count is larger than 0. I would say that making a separate property IsChild and IsParent is a bit redundant since it is already clear from other properties. If you only use it for the navigation Icons it's ok to keep it in the converter. If you use it for other things as well go with Blam's suggestion.

David
  • 853
  • 8
  • 24