ItemsControl 數(shù)據(jù)綁定的兩種方式
最近在學(xué)習(xí)ItemsControl這個控件的時候,查看了MSDN上面的一個例子,并且自己做了一些修改,這里主要使用了兩種方式來進行相應(yīng)的數(shù)據(jù)綁定,一種是使用DataContext,另外一種是直接將一個類綁定到前臺,其實這兩種方式原理差不多都是將數(shù)據(jù)模型的對象添加到一個ObservableCollection集合中,然后再綁定到前臺,下面分別介紹兩種綁定方式:
第一種
是將數(shù)據(jù)存儲在一個ObservableCollection集合中,然后作為ItemsControl的DataContext對象,下面分別貼出相關(guān)的代碼:
<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}"這句必須添加,否則后臺的數(shù)據(jù)是無法添加到前臺的,這個需要注意,這里貼出后臺的代碼
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.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;
}
}
}
這里最重要的一句就是 this.myItemsControl.DataContext = item;這個是將剛才的集合綁定到ItemsControl控件上,這里需要我們的注意。
第二種
另外一種方式的數(shù)據(jù)綁定就是將一個類綁定到這個ItemsControl控件上,具體的方式如下:
<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控件上面,這里貼出相關(guān)的代碼,我們定義了一個MyData的類,將該類作為數(shù)據(jù)源綁定到前臺上,這個類的代碼如下
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>,然后來添加相應(yīng)的數(shù)據(jù)源,我們在使用繼承的時候需要注意這些技巧。
其實這兩種情況都是將一個數(shù)據(jù)集合綁定到前臺,只不過是一些綁定的方式有所不同,實際的原理都是一樣的!
以上就是ItemsControl 數(shù)據(jù)綁定的兩種方式的詳細內(nèi)容,更多關(guān)于ItemsControl 數(shù)據(jù)綁定的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#獲取計算機硬件與操作系統(tǒng)的相關(guān)信息
這篇文章介紹了C#獲取計算機硬件與操作系統(tǒng)相關(guān)信息的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
C# XML字符串包含特殊字符的處理轉(zhuǎn)換方法小結(jié)
今天用C#輸出XML文件時,發(fā)現(xiàn)報錯,經(jīng)過反復(fù)檢查調(diào)試,發(fā)現(xiàn)是因為某處內(nèi)容含有某些特殊字符,這些特殊字符是在XML里不被允許的2020-07-07
WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案
在WPF中,默認情況下,DataContext是通過可視化樹來傳遞的,父元素的DataContext會自動傳遞給其子元素,以便子元素可以訪問父元素的數(shù)據(jù)對象,這篇文章主要介紹了WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案,需要的朋友可以參考下2023-08-08
C#實現(xiàn)OFD格式與PDF格式的互轉(zhuǎn)
OFD格式的文檔是一種我國獨有的國家標準版式的文檔。本文將通過C#程序介紹如何實現(xiàn)由OFD與PDF的互相轉(zhuǎn)換,感興趣的小伙伴可以了解一下2022-02-02

