1.DataTemplate这货是什么?
DataTemplate就是显示绑定数据对象的模板。
2.DataTemplate这货优点是什么?(为什么要使用DataTemplate)
“The WPF data templating model provides you with great flexibility to define the presentation of your data.”
就是说,为从数据展示(或者说是生成)界面提供了极大的灵活性。
(摘抄自:http://www.cnblogs.com/nankezhishi/archive/2009/07/08/datatemplate.html 文章内讲解了DataTemplate的基本原理和缺陷)3.一份黑暗料理的制作
嗯,我们先来看看效果 ,就是这样一个简单的列表
首先我们需要一个容器来存储我们自定义的DataTemplate 可以把这货想象成一张画满精美图案的菜单并写着制作方法和材料~ 当然上面并没有写图片仅供参考(QAQ)
/// <summary> /// 数据模板 /// </summary> public DataTemplate ItemDataTemplate { get { return (DataTemplate)GetValue(ItemDataTemplateProperty); } set { SetValue(ItemDataTemplateProperty, value); } } // Using a DependencyProperty as the backing store for ItemDataTemplate. This enables animation, styling, binding, etc... public static readonly DependencyProperty ItemDataTemplateProperty = DependencyProperty.Register("ItemDataTemplate", typeof(DataTemplate), typeof(DataTemplateDemo), new PropertyMetadata(null));
好的,菜单有了 怎么能没有原料呢。
/// <summary> /// 数据源 /// </summary> public IList ItemSource { get { return (IList)GetValue(ItemSourceProperty); } set { SetValue(ItemSourceProperty, value); } } // Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSource", typeof(IList), typeof(DataTemplateDemo), new PropertyMetadata(null, ItemSourceChanged)); private static void ItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((DataTemplateDemo)d).PreparePanel(); }
最后就差一个厨师了。
/// <summary> /// 准备显示面板 /// </summary> void PreparePanel() { for (int i = 0; i < ItemSource.Count; i++) { ContentPresenter contentPresenter = new ContentPresenter() { ContentTemplate = ItemDataTemplate, Content = ItemSource[i], Height = 50 }; ParentStackPanel.Children.Add(contentPresenter); } }
好的,一切都准备齐全了。你向小二大喊一声,我要一份黑暗料理!!! 但是小二并不知道该怎么做。没办法,只能自己给他讲解下。
<controls:DataTemplateDemo ItemSource="{Binding}" x:Name="dataTemplateDemo"> <controls:DataTemplateDemo.ItemDataTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition Height="9*"></RowDefinition> <RowDefinition Height="1*"></RowDefinition> </Grid.RowDefinitions> <Border BorderBrush="Purple" BorderThickness="1" Width="200"> <TextBlock Text="{Binding TestString}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Purple"></TextBlock> </Border> </Grid> </DataTemplate> </controls:DataTemplateDemo.ItemDataTemplate> </controls:DataTemplateDemo>
一通乱说后,小二终于明白了黑暗料理怎么做。去报告给厨师,并给了一张新的菜单给厨师。但是厨房里面根本没有所需要的材料,没办法只能自己提供给厨房
public class TestDataForDateTemplateDemo { public string TestString { get; set; } }
List<TestDataForDateTemplateDemo> DataTemplateDemoSource = new List<TestDataForDateTemplateDemo>(); for (int i = 0; i < 20; i++) { DataTemplateDemoSource.Add(new TestDataForDateTemplateDemo() { TestString = string.Format("我是1%中的第{0}个", i) }); } dataTemplateDemo.DataContext = DataTemplateDemoSource;
ok。你要的黑暗料理新鲜出炉了,请慢用~~~
最后,关于Binding。请参照这里:http://www.cnblogs.com/xh831213/p/3688258.html