我使用LongListSelector显示复杂对象列表,并根据绑定对象列表属性中的项目数更新datatemplate(控件)。 我尝试了以下内容。
希望明白我想要实现的目标。
如果有人能对我的问题有所了解,我会非常感激!
数据模板
<DataTemplate x:Key="LLS_SomeTemplate" >
<MyApp:ObjectTemplate SomeObjects="{Binding SomeEntities}"/>
</DataTemplate>
绑定对象
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()
{
}
}
控制属性
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
}
}
}
}
答案 0 :(得分:1)
CLR设置依赖项属性的方法很少。您必须避免在setter中执行操作。改为创建值更改事件处理程序:
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
}
}
}
希望它可以帮助您解决问题