在 UserControl 中实现 DataTemplate DependencyProperty

发布于 2024-12-06 07:18:24 字数 1069 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-12-13 07:18:24

如果不处理依赖属性的更改,那么依赖属性就毫无用处。
首先,您应该添加 PropertyChanged 回调。在我的示例中,我内联添加它们并调用 UpdateItems 私有方法。

public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems()));

public static readonly DependencyProperty ItemTemplateProperty = 
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems()));

然后,您可以调用 DataTemplate 类的 LoadContent 方法,并将 ItemsSource 中的项目设置为返回的可视元素的 DataContext:

private void UpdateItems()
{
    //Actually it is possible to use only the ItemsSource property,
    //but I would rather wait until both properties are set
    if(this.ItemsSource == null || this.ItemTemplate == null)
        return;

    foreach (var item in this.ItemsSource)
    {
        var visualItem = this.ItemTemplate.LoadContent() as FrameworkElement;
        if(visualItem != null)
        {
            visualItem.DataContext = item;
            //Add the visualItem object to a list or StackPanel
            //...
        }
    }
}

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.

public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems()));

public static readonly DependencyProperty ItemTemplateProperty = 
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems()));

Then you can call the LoadContent method of the DataTemplate class and set an item from the ItemsSource as the DataContext to the returned visual element:

private void UpdateItems()
{
    //Actually it is possible to use only the ItemsSource property,
    //but I would rather wait until both properties are set
    if(this.ItemsSource == null || this.ItemTemplate == null)
        return;

    foreach (var item in this.ItemsSource)
    {
        var visualItem = this.ItemTemplate.LoadContent() as FrameworkElement;
        if(visualItem != null)
        {
            visualItem.DataContext = item;
            //Add the visualItem object to a list or StackPanel
            //...
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文