【发布时间】:2011-09-23 13:13:01
【问题描述】:
我正在尝试使用 Expression Blend 4 在 Silverlight 4 中的 UserControl 中创建一个 intertia 触摸滚动列表。我已经在我的 UserControl 中创建了依赖属性,我想像 ListBox 一样工作。 ItemSource 是我想在我的列表中显示的对象列表,而 datatemplate 是它应该显示的方式。
如何在我的 UserControl 中处理这些属性?我有一个 StackPanel,应该在其中添加所有数据模板,以显示 c 的数据。
当循环通过 ItemSource 以将它们添加到列表 (StackPanel) 时,如何将 IEnumerable 中的数据应用到 DataTemplate。
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(InertiaScrollBox), null);
public IEnumerable ItemsSource
{
get{ return (IEnumerable)GetValue(ItemsSourceProperty); }
set{ SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(InertiaScrollBox), null);
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
这有点难以解释,但希望您能理解,否则请询问。提前致谢
【问题讨论】:
-
似乎您最好将 ListBox 子类化或使用行为。通常依赖属性用于 Control 而不是 UserControl。
-
使用控件模板或继承现有控件可能会更好,但我们并不了解所有细节,问题是关于数据模板。
-
我正在创建一个触摸惯性滚动列表。子类化 ListBox 使我很难捕捉到所有鼠标事件,因为默认情况下,鼠标左键被 ListBox 内的 ListBoxItem 控件捕捉到。也许可以做到,但我认为它会比我现在得到的更复杂,效果很好:)
标签: silverlight user-controls datatemplate dependency-properties