在 UserControl 中实现 DataTemplate DependencyProperty
我正在尝试使用 Expression Blend 4 在 Silverlight 4 的 UserControl 中创建一个惯性触摸滚动列表。我已经在我的 UserControl 中创建了依赖属性,我希望它像 ListBox 一样工作。 ItemSource 是我想要在列表中显示的对象列表,而 datatemplate 是它应该显示的方式。
我如何处理 UserControl 中的这些属性?我有一个 StackPanel,其中应添加所有数据模板来显示数据。
当循环通过 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); }
}
这有点难以解释,但希望你能理解,否则请询问。提前致谢
I'm trying to make an intertia touch scrolling list in a UserControl in Silverlight 4 using Expression Blend 4. I've already made the dependency properties in my UserControl which i want to work like the ListBox does. ItemSource is the list of objects i want to show in my list and datatemplate is the way it should be shown.
How do i deal with these properties inside my UserControl? I have a StackPanel where all the datatemplates should be added showing the data ofc.
How do i apply the data in my IEnumerable to the DataTemplate when looping through the ItemSource to add them to the list (StackPanel).
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); }
}
This was kinda hard to explain but hope you understand otherwise please ask. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不处理依赖属性的更改,那么依赖属性就毫无用处。
首先,您应该添加 PropertyChanged 回调。在我的示例中,我内联添加它们并调用
UpdateItems
私有方法。然后,您可以调用
DataTemplate
类的LoadContent
方法,并将ItemsSource
中的项目设置为返回的可视元素的 DataContext:Dependency properties are rather useless if to not handle their changes.
At first you should add PropertyChanged callbacks. In my example I add them inline and call the
UpdateItems
private method.Then you can call the
LoadContent
method of theDataTemplate
class and set an item from theItemsSource
as the DataContext to the returned visual element: