简而言之,我要做的是创建一个数据模板来指示客户横幅的外观。
我以非常简单的形式工作,但仅使用 ListView 控件,我将 ItemsSource 应用于包含一个条目的列表。
我想做的是将 Customer 对象直接应用于控件(不确定控件的类型),然后它选取此类型的 DataTemplate 并对数据进行布局。
我正在使用的 xaml 是...
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Customer}" >
<Border Background="Blue" >
<TextBlock Text="{Binding CustomerName}" />
</Border>
</DataTemplate>
</Window.Resources>
<ListView x:Name="mylist" />
</Window>
使用以下代码隐藏。
namespace WpfApplication5
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Customer mp=new Customer();
mp.CustomerName="Mr. Banana";
List<Customer> temp = new List<Customer>();
temp.Add(mp);
mylist.ItemsSource = temp;
}
}
public class Customer
{
public string CustomerName { get; set; }
}
}
只需使用ContentControl
:
<ContentControl x:Name="banner" />
并在代码中:
banner.Content = mp;
创建您自己的UserControl
,其中包含可用于绑定Customer
属性。然后,可以使用RelativeSource
绑定绑定到该属性。