欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ItemsControl 數(shù)據(jù)綁定的兩種方式

 更新時(shí)間:2021年03月03日 16:06:21   作者:Hello——尋夢(mèng)者!  
這篇文章主要介紹了ItemsControl 數(shù)據(jù)綁定的兩種方式,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

     最近在學(xué)習(xí)ItemsControl這個(gè)控件的時(shí)候,查看了MSDN上面的一個(gè)例子,并且自己做了一些修改,這里主要使用了兩種方式來(lái)進(jìn)行相應(yīng)的數(shù)據(jù)綁定,一種是使用DataContext,另外一種是直接將一個(gè)類綁定到前臺(tái),其實(shí)這兩種方式原理差不多都是將數(shù)據(jù)模型的對(duì)象添加到一個(gè)ObservableCollection集合中,然后再綁定到前臺(tái),下面分別介紹兩種綁定方式:

第一種

是將數(shù)據(jù)存儲(chǔ)在一個(gè)ObservableCollection集合中,然后作為ItemsControl的DataContext對(duì)象,下面分別貼出相關(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}"這句必須添加,否則后臺(tái)的數(shù)據(jù)是無(wú)法添加到前臺(tái)的,這個(gè)需要注意,這里貼出后臺(tái)的代碼 

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;這個(gè)是將剛才的集合綁定到ItemsControl控件上,這里需要我們的注意。

第二種

  另外一種方式的數(shù)據(jù)綁定就是將一個(gè)類綁定到這個(gè)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>

這里我們通過資源來(lái)引用一個(gè)類,讓后使用  ItemsSource="{Binding Source={StaticResource myDataSource}}"將這個(gè)類綁定到ItemsSource控件上面,這里貼出相關(guān)的代碼,我們定義了一個(gè)MyData的類,將該類作為數(shù)據(jù)源綁定到前臺(tái)上,這個(gè)類的代碼如下

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"
    }
    );
  
  }
  
 }
}

  這里面很重要的一部就是讓這個(gè)類繼承自 ObservableCollection<DataSource>,然后來(lái)添加相應(yīng)的數(shù)據(jù)源,我們?cè)谑褂美^承的時(shí)候需要注意這些技巧。

  其實(shí)這兩種情況都是將一個(gè)數(shù)據(jù)集合綁定到前臺(tái),只不過是一些綁定的方式有所不同,實(shí)際的原理都是一樣的!

以上就是ItemsControl 數(shù)據(jù)綁定的兩種方式的詳細(xì)內(nèi)容,更多關(guān)于ItemsControl 數(shù)據(jù)綁定的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼

    C#創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼

    本篇文章主要介紹了C# 創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-05-05
  • 淺析C#中數(shù)組,ArrayList與List對(duì)象的區(qū)別

    淺析C#中數(shù)組,ArrayList與List對(duì)象的區(qū)別

    在C#中,當(dāng)我們想要存儲(chǔ)一組對(duì)象的時(shí)候,就會(huì)想到用數(shù)組,ArrayList,List這三個(gè)對(duì)象了。那么這三者到底有什么樣的區(qū)別呢
    2013-07-07
  • C#如何通過RFC連接sap系統(tǒng)

    C#如何通過RFC連接sap系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#如何通過RFC連接sap系統(tǒng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解

    Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解

    這篇文章主要為大家介紹了Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • c# 對(duì)windows用戶和組操作實(shí)例

    c# 對(duì)windows用戶和組操作實(shí)例

    c# 對(duì)windows用戶和組操作實(shí)例,需要的朋友可以參考一下
    2013-04-04
  • 基于C#實(shí)現(xiàn)Ping工具類

    基于C#實(shí)現(xiàn)Ping工具類

    Ping是一種常用的測(cè)試網(wǎng)絡(luò)連接的工具,可以測(cè)試網(wǎng)絡(luò)延遲和連接狀況,以及判斷網(wǎng)絡(luò)是否可用,本文將通過框架類庫(kù)中的Ping類來(lái)實(shí)現(xiàn)Ping功能,感興趣的小伙伴可以了解下
    2023-11-11
  • C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能

    C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能

    這篇文章主要為大家詳細(xì)介紹了C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • C#使用ADO.Net連接數(shù)據(jù)庫(kù)與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問

    C#使用ADO.Net連接數(shù)據(jù)庫(kù)與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問

    這篇文章介紹了C#使用ADO.Net連接數(shù)據(jù)庫(kù)與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#的泛型方法解析

    C#的泛型方法解析

    本文講解了C#2.0引入的泛型知識(shí),主要包含泛型類、泛型接口、泛型委托,并且重點(diǎn)講解了泛型方法,已經(jīng)泛型的約束分類。最后給了一些利用泛型方法操作xml的方法。希望對(duì)大家有所幫助
    2016-12-12
  • C#中枚舉的特性 FlagAttribute詳解

    C#中枚舉的特性 FlagAttribute詳解

    說(shuō)到FlagsAttribute,源自前幾天看到了一小段代碼,大概意思就是根據(jù)航班政策來(lái)返回哪些配送方式是否可用,根據(jù)這些是否可用來(lái)隱藏或者開啟界面的相關(guān)配送方式,不是非常明白,于是今天我們就來(lái)詳細(xì)探討下這個(gè)問題
    2018-03-03

最新評(píng)論