Question

I'm using a LongListSelector to display a list of complex objects and update the datatemplate (a control) depending on the number of items in the bound objects list property. I have tried the following.

  • Accessing the data in the OnItemRealized event to try and get the bound control and update it via a method call. Not sure if possible
  • Adding a property to the control being bound which adds controls to a wrap panel when the property is set. The set accessor in the controls property is never hit.

Hopefully it's clear what im trying to achieve.

  1. Is it possible to call functionality in a property being bound to as shown in my control
  2. If not is it possible to access the control being bound and call exposed functionality that way

If anyone could shed any light on my issue i would greatly appreciate it!

Data Template

<DataTemplate x:Key="LLS_SomeTemplate" >
            <MyApp:ObjectTemplate SomeObjects="{Binding SomeEntities}"/>
</DataTemplate>

Object being bound

public class SomeObject
{
    public ObservableCollection<Entities> _SomeEntities { get; set; }
    public ObservableCollection<Entities> SomeEntities
    {
        get
        {
            if (_SomeEntities == null)
                _SomeEntities = new ObservableCollection<Entities>();

            return _SomeEntities;
        }
        set
        {
            _SomeEntities = value;
        }
    }

    public SomeObject()
    {
    }
}

Control Property

public static DependencyProperty SomeObjectsProperty = DependencyProperty.Register("SomeObjects", typeof(ObservableCollection<Entities>), typeof(ObjectTemplate), new PropertyMetadata(new ObservableCollection<Entities>()));

public ObservableCollection<SomeObject> SomeObjects

    {
    get
    {
        return (ObservableCollection<SomeObject>)GetValue(SomeObjectsProperty);
    }
    set
    {
        SetValue(SomeObjectsProperty, value);

        if (value != null && value.Count > 0)
        {
            foreach (SomeObject eLink in value)
            {
                //Add a new control to a wrap panel for each object in the list
            }
        }
    }
    }
Était-ce utile?

La solution

There are few ways how CLR set up dependency properties. You must avoid perform operations in setter. Create value changed event handler instead:

public static DependencyProperty SomeObjectsProperty = DependencyProperty.Register("SomeObjects", typeof(ObservableCollection<Entities>), typeof(ObjectTemplate), new PropertyMetadata(new ObservableCollection<Entities>(), new PropertyChangedCallback(OnSomeObjectsPropertyChanged));

private static void OnSomeObjectsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (d as ObjectTemplate).UpdateSomeObjects(e.NewValue as SomeObjects);
}

public void UpdateSomeObjects(SomeObjects value)
{
    if (value != null && value.Count > 0)
    {
        foreach (SomeObject eLink in value)
        {
            //Add a new control to a wrap panel for each object in the list
        }
    }
}

Hope it helps you to solve your problem

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top