【问题标题】:Can't bind ItemsControl.ItemTemplate to List<object> element无法将 ItemsControl.ItemTemplate 绑定到 List<object> 元素
【发布时间】:2015-03-23 08:58:35
【问题描述】:

我有麻烦了。我创建了如下所示的 UserControl:

http://i.stack.imgur.com/ITxkS.jpg

我有绑定到 UserControl 的 TextBlock.Text 的依赖属性“Text”。我想创建另一个可视化列表的用户控件。这是我的代码:

<UserControl x:Class="ListPresenter.ListViewer"
         xmlns:dop="clr-namespace:DeletableObjectPresenter;assembly=DeletableObjectPresenter"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
      <ItemsControl ItemsSource="{Binding List, RelativeSource={RelativeSource AncestorType=UserControl}}">
          <ItemsControl.ItemsPanel>
             <ItemsPanelTemplate>
                 <WrapPanel></WrapPanel>
             </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.ItemTemplate>
              <ItemContainerTemplate>
                  <dop:DeletableObjectPresenter></dop:DeletableObjectPresenter>
              </ItemContainerTemplate>
          </ItemsControl.ItemTemplate>
      </ItemsControl>
</Grid>

...

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ListPresenter
{
    /// <summary>
    /// Interaction logic for ListViewer.xaml
    /// </summary>
    public partial class ListViewer : UserControl
    {
        public static readonly DependencyProperty ListProperty =                     DependencyProperty.Register(
        "List", typeof (IList<object>), typeof (ListViewer), new PropertyMetadata(null));
        public List<object> List 
        {
            get { return (List<object>) GetValue(ListProperty); }
            set { SetValue(ListProperty, value); }
        }
        public ListViewer()
        {
            InitializeComponent();
        }
    }
}

这是我的控件的外观:

http://i.stack.imgur.com/JIjHy.jpg

问题是我不知道如何将 Item 的 Text 绑定到列表的元素。谢谢!

【问题讨论】:

  • 用 DataTemplate 替换 ItemContainerTemplate

标签: wpf list xaml binding itemscontrol


【解决方案1】:

首先要做的是在您的UserControl 中为您的Text 属性创建一个DependencyProperty。您应该在UserControl 中将数据绑定到此属性:

<TextBlock Text="{Binding Text, RelativeSource={RelativeSource 
    AncestorType={x:Type dop:DeletableObjectPresenter}}}" />

现在您可以从ItemsControl.ItemTemplate 数据绑定到UserControlText 属性:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <dop:DeletableObjectPresenter Text="{Binding PropertyToBindToText}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

当然,List 集合中的对象需要有一个属性(在本例中名为 PropertyToBindToText)才能数据绑定到 UserControlText 属性。因此,您最好创建一个强类型集合而不是您的 List&lt;object&gt;,此外,习惯上在 WPF 中使用 ObservableCollection&lt;T&gt;

有关详细信息,请参阅 MSDN 上的 Data Binding Overview 页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2014-01-07
    • 2011-11-17
    • 2015-09-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多