最近在學習ItemsControl這個控件的時候,查看了MSDN上面的一個例子,而且本身作了一些修改,這裏主要使用了兩種方式來進行相應的數據綁定,一種是使用DataContext,另一種是直接將一個類綁定到前臺,其實這兩種方式原理差很少都是將數據模型的對象添加到一個ObservableCollection集合中,而後再綁定到前臺,下面分別介紹兩種綁定方式:學習
第一種是將數據存儲在一個ObservableCollection集合中,而後做爲ItemsControl的DataContext對象,下面分別貼出相關的代碼:this
<Window x:Class="TestGrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestGrid" Title="MainWindow" Height="350" Width="525"> <Grid> <ItemsControl Margin="10" x:Name="myItemsControl" ItemsSource="{Binding}"> <ItemsControl.Template> <ControlTemplate TargetType="ItemsControl"> <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15"> <ItemsPresenter/> </Border> </ControlTemplate> </ItemsControl.Template> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="{ x:Type local:DataSource}"> <DataTemplate.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="18"/> <Setter Property="HorizontalAlignment" Value="Center"/> </Style> </DataTemplate.Resources> <Grid> <Rectangle Fill="Green"/> <Ellipse Fill="Red"/> <StackPanel> <TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/> <TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/> </StackPanel> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Control.Width" Value="100"/> <Setter Property="Control.Margin" Value="5"/> <Style.Triggers> <Trigger Property="Control.IsMouseOver" Value="True"> <Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/> </Trigger> </Style.Triggers> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </Grid> </Window>
這裏須要注意的地方是 ItemsSource="{Binding}"這句必須添加,不然後臺的數據是沒法添加到前臺的,這個須要注意,這裏貼出後臺的代碼 spa
using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TestGrid { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { private ObservableCollection<DataSource> item = null; public MainWindow() { InitializeComponent(); item = new ObservableCollection<DataSource>(); item.Add( new DataSource() { Priority = "1", TaskName = "hello" } ); item.Add( new DataSource() { Priority = "2", TaskName = "whats" } ); item.Add( new DataSource() { Priority = "3", TaskName = "your" } ); item.Add( new DataSource() { Priority = "4", TaskName = "name" } ); item.Add( new DataSource() { Priority = "5", TaskName = "can" } ); item.Add( new DataSource() { Priority = "6", TaskName = "you" } ); this.myItemsControl.DataContext = item; } } }
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestGrid { public class DataSource : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public DataSource() { } private string priority; public string Priority { get { return priority; } set { priority = value; OnPropertyChanged("Priority"); } } private string taskname; public string TaskName { get { return taskname; } set { taskname = value; OnPropertyChanged("TaskName"); } } protected void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } private List<DataSource> datasources; public List<DataSource> DataSources { get { return datasources; } set { datasources = value; } } } }
這裏最重要的一句就是 this.myItemsControl.DataContext = item;這個是將剛纔的集合綁定到ItemsControl控件上,這裏須要咱們的注意。code
另一種方式的數據綁定就是將一個類綁定到這個ItemsControl控件上,具體的方式以下:xml
<Window x:Class="TestGrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestGrid" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:MyData x:Key="myDataSource"/> </Window.Resources> <Grid> <ItemsControl Margin="10" x:Name="myItemsControl" ItemsSource="{Binding Source={StaticResource myDataSource}}"> <ItemsControl.Template> <ControlTemplate TargetType="ItemsControl"> <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15"> <ItemsPresenter/> </Border> </ControlTemplate> </ItemsControl.Template> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="{ x:Type local:DataSource}"> <DataTemplate.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="18"/> <Setter Property="HorizontalAlignment" Value="Center"/> </Style> </DataTemplate.Resources> <Grid> <Rectangle Fill="Green"/> <Ellipse Fill="Red"/> <StackPanel> <TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/> <TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/> </StackPanel> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Control.Width" Value="100"/> <Setter Property="Control.Margin" Value="5"/> <Style.Triggers> <Trigger Property="Control.IsMouseOver" Value="True"> <Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/> </Trigger> </Style.Triggers> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </Grid> </Window>
這裏咱們經過資源來引用一個類,讓後使用 ItemsSource="{Binding Source={StaticResource myDataSource}}"將這個類綁定到ItemsSource控件上面,這裏貼出相關的代碼,咱們定義了一個MyData的類,將該類做爲數據源綁定到前臺上,這個類的代碼以下對象
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestGrid { public class MyData:ObservableCollection<DataSource> { public MyData() { Add (new DataSource() { Priority = "1", TaskName = "My" } ); Add(new DataSource() { Priority = "2", TaskName = "Name" } ); Add(new DataSource() { Priority = "3", TaskName = "Is" } ); Add(new DataSource() { Priority = "4", TaskName = "Ye" } ); Add(new DataSource() { Priority = "5", TaskName = "Bo" } ); } } }
這裏面很重要的一部就是讓這個類繼承自 ObservableCollection<DataSource>,而後來添加相應的數據源,咱們在使用繼承的時候須要注意這些技巧。blog
其實這兩種狀況都是將一個數據集合綁定到前臺,只不過是一些綁定的方式有所不一樣,實際的原理都是同樣的!繼承