【发布时间】: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